Given the following code declaring and initializing two variables and with respective 1. int a b
values 3 and 5, indicate whether the value of each expression is or . true false
int a = 3;
int b = 5;
a<b
a != b
a == 4
(b - a) <= 1
Math.abs(a - b) >= 2
(b % 2 == 1)
b <= 5
true: a<b, a != b, Math.abs(a - b) >= 2, (b % 2 == 1), b <= 5
false: a == 4, (b - a) <= 1,
1/47
Term | Definition |
---|---|
Given the following code declaring and initializing two variables and with respective 1. int a b values 3 and 5, indicate whether the value of each expression is or . true false int a = 3; int b = 5; a<b a != b a == 4 (b - a) <= 1 Math.abs(a - b) >= 2 (b % 2 == 1) b <= 5 | true: a<b, a != b, Math.abs(a - b) >= 2, (b % 2 == 1), b <= 5 false: a == 4, (b - a) <= 1, |
indicate whether the value of each expression is true or false. boolean a = true; boolean b = true; boolean c = false; !a a && b a && c a || c !(a || b) !a || b !(!(a && c)) a && !(b || c) | true: a && b, a || c, !a || b false: !a, a && c, !(a || b), !(!(a && c)), a && !(b || c) |
3. Given two boolean variables a and b, are the following expressions equivalent? # !(!a) # a | yes |
Given two variables, are the following expressions equivalent? # !(a && b) # !a || !b | no |
Given two variables and , are the following expressions equivalent? boolean a b # !(!a && !b) # a && b | no |
Given two variables and , are the following expressions equivalent? boolean a b # !(!a && !b) # a || b | yes |
int a = 3; int b = 5; true or false # a < b || b < 10 a != b && b < 10 # a == 4 || b < 10 # a > b && b < 10 | true true true false |
Mark all the valid Java selection keywords. # if # else if # else # elsif | all good except that last one |
How do we compare the value of two String objects in Java? (Mark all that apply.) # using the = operator # using the == operator # using the equals method | last two |
What is the output of this code sequence? boolean a = true; System.out.println a; | true |
11. What is the output of this code sequence? boolean a = (true && false); System.out.println a ; | some sort of error, that or it will say false? |
What is the output of this code sequence? if (true || false) && (true || false) System.out.println("Inside true block"); System.out.println("End of sequence"); | It will print both lines, becuase there is no else statment |
What is the output of this code sequence? if( 27 % 3 === 0) System.out.println("27 is divisible by 3"); else System.out.println("27 is not divisible by 3") System.out.println("End of sequence"); | will tell us if 27 is dividable by 3 |
What is the output of this code sequence? int grade = 77; if (grade >= 90) System.out.println("A"); else if(grade >= 80) System.out.println("B"); else if(grade >= 70) System.out.println("C"); else System.out.println("D or lower"); | will print C |
If a is true, increment b by 1 | if (a == true) b += 1; |
If a is true, increment b by 2; if a is false, decrement b by 1. | if (a == true) b += 2; else b -= 1; |
If a is true, change a to false; if a is false, change a to true. | if (a == true) a = false; else a = true; |
If b is equal to c, then assign true to a. | if (b == c) a = true; |
If b is less than c, increment b by 1; otherwise, leave b unchanged. | if (b < c) b += 1; |
If b is a multiple of c, set a to true; otherwise, set a to false. | if (b % c == 0) a = true; else a = false; |
If c is not equal to 0, assign to b the value of b divided by c. | if (c != 0) b /= c; |
If the product b times c is greater than or equal to 100, then invert a's value (true -> false); otherwise, assign a to true. | if (b * c >= 100) { if (a == true) a = false else a = true; } else { a = true; } |
If a is true and b is greater than 10, increment c by 1. | if (a == true && b > 10) { c += 1; } |
If both b and c are less than 10, then assign true to a; otherwise, assign false to a. | if (b < 10 && c < 10) { a = true; } else { a = false: ] |
If b or c is greater than 5, then assign true to a; otherwise, assign false to a. | if (b > 5 || c > 5) { a = true; else { a = false; } |
b1 b2 = bool a1 a2 = int Where is the error in this code sequence? b1 = a1 && a2; | ints dont have any truth or false to them |
b1 b2 = bool a1 a2 = int Where is the error in this expression? ( b2 == b1) AND (a1 <= a2 ) | and should be && |
b1 b2 = bool a1 a2 = int Where is the logical error in this code sequence? if (a1 == 4); System.out.println("a1 equals 4"); | ); <- no ; after the if condition |
b1 b2 = bool a1 a2 = int Where is the error in this code sequence? boolean b1 = true; if b1 System.out.println("b1 is true"); | there is no condition of this if statement |
b1 b2 = bool a1 a2 = int Where is the error in this code sequence? if b2 == { true} System.out.println("b2 is true"); | {} bro, no curly here |
b1 b2 = bool a1 a2 = int Where is the error in this code sequence? if (b1 == true) System.out.println("b1 is true"); else System.out.println("b1 is false"); else if(a1 < 100) System.out.println("a1 is <= 100"); | ended on else if, should end on else |
b1 b2 = bool a1 a2 = int Is there an error in this code sequence? Explain. if (b2 == b1) System.out.println("b2 and b1 have the same value"); else if (a1 == a2) System.out.println("a1 and a2 have the same value"); else System.out.println("All vars are diff."); | dont think so |
b1 b2 = bool a1 a2 = int if (b2) System.out.println("b2 is true"); else if(a1 <= 10 || a2 > 50) { System.out.print("a1 <= 10 or "); System.out.println("a2 > 50"); } else System.out.println("none of the above"); | not condition on that first if |
You coded the following in the class Test.java: int a = 32; if( a = 31) // line 9 System.out.println("The value of a is 31"); else System.out.println("The value of a is not 31"); At compile time, you get the following error: Test.java:9: error: incompatible types if ( a = 31 ) // line 9 ^ 1 error Explain what the problem is and how to fix it. | assignment operator, should be == not = |
Write a program that takes two ints as input from the keyboard, representing the number of hits and the number of at-bats for a batter. Then calculate the batter’s hitting percentage and check if the hitting percentage is above .300. If it is, output that the player is eligible for the All Stars Game; otherwise, output that the player is not eligible. | import java.util.Scanner; public class App{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("How many hits were there?: "); int hits = scan.nextInt(); System.out.print("How many at-bats were there?: "); int bats = scan.nextInt(); double avg = (double) hits / bats; if (avg > .300) { System.out.println("This player is eligible!"); } else { System.out.println("This player is ineligible!"); } } } |
Write a program that reads a char as an input from the keyboard and outputs whether it is a valid character to start an identifier. (Hint: look for a method in the Character class.) | import java.util.Scanner; public class App{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); String preChar = scan.next(); char letter = preChar.charAt(0); if (Character.isLetter(letter) == true) System.out.println("Valid"); else { System.out.println("Invalid"); } } } |
What is the output of this code sequence? String s = "Hello"; if(s.equals("hello")) System.out.println("String is hello"); else System.out.println("String is not hello"); | String is not hello |
What is the output of this code sequence int a = 65; boolean b = false; if(a >= 70) { System.out.println("Hello 1"); if (b == true) System.out.println("Hello 2"); } else { System.out.println("Hello 3"); if(b == false) System.out.println("Hello 4"); } | Hello 3 Hello 4 |
What is the output of this code sequence? int season = 3; switch(season) { case 1: System.out.println("Season is Winter"); break; case 2: System.out.println("Season is Spring"); break; case 3: System.out.println("Season is Summer"); break; case 4: System.out.println("Season is Fall"); break; default: System.out.println("Invalid season"); } | Season is Summer |
What is the output of this code sequence? char c = 'e'; switch(c) { case'H': System.out.println("letter 1"); break; case'e': System.out.println("letter 2"); break; case'l': System.out.println("letters 3 and 4"); break; case'o': System.out.println("letter 5"); break; default: System.out.println("letter is not in Hello"); } | letter |
What is the output of this code sequence? int n = 3; switch(n ) { case 1: System.out.println("Number 1"); case 2: System.out.println("Number 2"); case 3: System.out.println("Number 3"); case 4: System.out.println("Number 4"); default: System.out.println("Other number"); } | Number |
You coded the following in class Test.java: boolean b = true; if(b) System.out.println("Inside true block"); System.out.println("b was true"); System.out.println("Inside false block"); At compile time, you get the following error: Test.java:12: error: 'else' without 'if' else // line 12 ^ 1 erro | missing {} for the if statement made the whole thing wacky broski |
41. You coded the following in the class boolean b = true; if(b) { System.out.println("Inside true block"); System.out.println("b was true"); else System.out.println("Inside false block"); } At compile time, you get the following error: Test.java:13: error: 'else' without 'if'. else // line 13 ^ 1 error Explain what the problem is and how to fix it. | incorrect {} placement |
Write a program that calculates the area of the following figures: # a square of side 0.666666667 # a rectangle of sides 1/9 and 4 | public class App{ public static void main(String[] args) { final double SQUARE_SIDE = 0.666666667; final double REC_SIDE_1 = 1.0 / 9; final int REC_SIDE_2 = 4; System.out.println("test = " + REC_SIDE_1); double squareArea = SQUARE_SIDE * SQUARE_SIDE; double recArea = REC_SIDE_1 * REC_SIDE_2; System.out.println("Area of square = " + squareArea); System.out.println("Area of rec = " + recArea); } } |
Write a program that reads a sentence from the keyboard. Depending on the last character of the sentence, print a message identifying the sentence as declarative (ends with a period), interrogative (ends with a question mark), exclamatory (ends with an exclamation point), or other. | String typed = scan.next(); char symbol = typed.charAt(typed.length() - 1); switch (symbol) { case'.': System.out.println("A plain sentence."); break; case'!': System.out.println("An exclamation!"); break; case'?': System.out.println("A question?"); break; default: System.out.println("Do you punctuate your sentences or nah"); break; } |
Write a program that takes two words as input from the keyboard, representing a password and the same password again. (Often, websites ask users to type their password twice when they register to make sure there was no typo the first time around.) Your program should do the following: # if both passwords match, then output “You are now registered as a new user” # otherwise, output “Sorry, there is a typo in your password” | import java.util.Scanner; public class App{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Type your password: "); String pass1 = scan.next(); System.out.println("Type your password: "); String pass2 = scan.next(); if (pass1.equals(pass2)) { System.out.println("GG"); } else { System.out.println("Nope"); } } } |
When comparing two doubles or floats for equality, programmers calculate the difference between the two numbers and check if that difference is sufficiently small. Explain why and give a real-life example. | rounding errors, so you have to make sure the difference is negligible because computers do math dumbly |