cs exercise 2
What's the value of tax after the execution of the following code segment: double tax = 0, price = 100, category = 3; tax = price * 0.05; if (category == 1) tax += price * 0.02; else if (category == 2) tax += price * 0.03; else if (category == 3) tax += price * 0.05;
10.0
What's the output of this Java code when 19 is entered? public static void main(String[] args) { Scanner input = new Scanner(System.in); int userAge = input.nextInt(); switch (userAge){ case 18: System.out.println("18"); break; case 19: System.out.println("19"); case 20: System.out.println("20"); break; default: System.out.println("Not in 18, 19, or 20"); } }
19 20
What is the value of x after executing following code? int x = 2; if (x < 4) { x = x + 1; } else { x = 2 * x; } 3 2 4 5
3
What is the output of the following code segment? public static void main(String[] args) { System.out.println(numOfDays(12, 2000)); System.out.println(numOfDays(2, 2000)); System.out.println(numOfDays(4, 2000)); System.out.println(numOfDays(12, 2001)); System.out.println(numOfDays(2, 2001)); System.out.println(numOfDays(4, 2001)); } public static int numOfDays(int month, int year) { int days; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; case 2: if (isLeap(year)) { days = 29; } else { days = 28; } break; default: days = 0; System.out.println("Not valid input"); } return days; } public static boolean isLeap(int year) { return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0); }
31 29 29 31 28 28
What is the value of x after executing following code? int x = 4; if (x < 4) { x = x + 1; } else { x = 2 * x; } 8 5 10 4
8
What's the output from the following code? int x = 5; if (x >= 5) System.out.println(x); x++; System.out.println(x); else System.out.println(x - 5); A. There is a compile error. B. 5 C. 0 D. 5 6
A. There is a compile error.
Which of the following have compile errors? Check all that apply. A. int x = 5; if (x >= 5) { System.out.println(x + 5); else System.out.println(x - 5); } B. int x = 5; if (x >= 5) { System.out.println(x + 5); } else { System.out.println(x - 5); } C. int x = 5; if (x >= 5) { i++; System.out.println(x + 5); } else System.out.println(x - 5); D. int x = 5; if (x >= 5) { System.out.println(x + 5); } else System.out.println(x - 5);
A. int x = 5; if (x >= 5) { System.out.println(x + 5); else System.out.println(x - 5); }
What's the output of the following code segments? int num = 6; if (num % 3 == 0) System.out.println("multiple of 3"); if (num % 4 == 0) System.out.println("multiple of 4"); System.out.println("*multiple of 4"); else System.out.println("NOT multiple of 4"); A. multiple of 3 NOT multiple of 4 B. There is a compile error. C. multiple of 3 D. multiple of 3 multiple of 4 *multiple of 4
B. There is a compile error
if int n1 = 12, n2 = 5, what's the result of the following expression? !(n1 < 10) && (n2 < n1) A. False B. True
B. True
What is the output of the following code? double pricePerSq = 125; if (pricePerSq > 120) System.out.println("expensive"); if (pricePerSq > 90) System.out.println("normal"); else System.out.println("economical"); A. normal economical B. expensive normal C. normal D. expensive normal economical
B. expensive normal
Which of the following expression determines whether or not x is a valid value for the following function √x2−4/x−3 A. x != 3 && x => 2 && x =< -2 B. x != 3 && (x >= 2 || x <= -2) C. x != 3 || (x > 2 || x < -2) D. x >3 || x <= -2
B. x != 3 && (x >= 2 || x <= -2)
Given x is variable that has been initialized with a number, which of the following expressions are always true? A. x > 0 || x < 0 B. x > 0 || x <= 0 C. x > 0 && x < 0 D. x * x > 0
B. x > 0 || x <= 0
Which of the following expression is equivalent to x != y? A. (x = y) B. x > y && x < y C. x > y || x < y D. x >= y || x <= y
C. x > y || x < y
Which of the following expressions are equivalent to x != y? A. x > y && x < y B. x > y || x < y C. x >= y || x < y D. ! (x == y) E. x >= y || x <= y
D. ! (x == y) B. x > y || x < y
Given both m and n have been declared and initialized, and max has been declared, which if-else statement is invalid to assign the max m and n to variable max? A. if (m > n) max = m; else max = n; B. if (m > n) max = m; else { max = n; } C. if (m > n) { max = m; } else { max = n; } D. if m > n max = m; else max = n;
D. if m > n max = m; else max = n;
Which of the following are valid types for the expression in a switch statement? Check all that apply. float String char int
String char int
For the following code segment switch(m) { case 1: System.out.print("a"); break; case 2: System.out.print("b"); default: System.out.print("c");} what is the put when the value of m is 1? a ab c abc
a
For the following code segment switch(m) { case 1: System.out.print("a"); break; case 2: System.out.print("b"); default: System.out.print("c"); } what is the put when the value of m is 2? abc bc b a
bc
For the following code segment switch(m) { case 1: System.out.print("a"); break; case 2: System.out.print("b"); default: System.out.print("c"); } what is the put when the value of m is 3? a b bc c
c
Which keywords may be used in a switch statement? case default break switch
case default break switch
What's the output of the following code segment? boolean check = false; if (check = true) System.out.println("check is true"); if(check = false) System.out.println("check is false"); check is true check is true check is false compile error check is false
check is true
What is the output of the following code snippet? boolean even = false;System.out.println((even ? "odd" : "even")); even odd:even cannot determine. odd
even
What's the value of comfortable?double temp = 75;boolean comfortable = (temp < 75 && temp > 50) ? true : false; false true 1 0
false
Rewrite the following statement with conditional operators to an equivalent if-else statement. x and y have been declared and initialized, and abs has been declared. abs = x > y ? x - y : y - x;
if (x > y) abs = x-y; else abs = y-x;
What's the output of the following code snippet? int x = 10; if (x > 12); System.out.print("large"); large There is no compile error and there is no output. There is a compile error.
large
What's the output of the following code segments? int num = 6; if (num % 3 == 0) System.out.println("multiple of 3"); else if (num % 2 == 0) System.out.println("multiple of 2"); else System.out.println("NOT multiple of 4");
multiple of 3
What's the output of the following code segments? int num = 6; if (num % 3 == 0) System.out.println("multiple of 3"); if (num % 4 == 0) System.out.println("multiple of 4"); else System.out.println("NOT multiple of 4");
multiple of 3 NOT multiple of 4
What is the output of the following code? int num = 12; if (num % 3 == 0) { if (num % 5 == 0) { System.out.println("multiple of 15"); } else { System.out.println("multiple of 3"); System.out.println("not multiple of 5"); } } else { System.out.println("not multiple of 3"); } multiple of 15 no output not multiple of 3 multiple of 3 not multiple of 5
multiple of 3 not multiple of 5
What's the value of the following variables? int num = 6; boolean multiple = num % 2 == 0 && num % 3== 0; boolean condition = num > 6 || num < 6; boolean notChanged = num == num++; multiple: condition: notChanged;
multiple: true condition: false notChanged: true
What is the output of the following code? double pricePerSq = 95; if (pricePerSq > 120) System.out.println("expensive"); else if (pricePerSq > 90) System.out.println("normal"); else System.out.println("economical"); expensive normal economical expensive normal normal normal economical
normal
What's the output of the following code segment? boolean found = true; boolean done = false; if (!found || done) System.out.println("lost"); else System.out.print("to be continued");
to be continued
What is the output of the following code snippet? int x = 0; if (x < 10) { x = x + 10; } System.out.println("x is " + x); x is 10 x is 0 x is 30 x is 20
x is 10
What is the output of the following code snippet? int x = 10;if (x < 10) { x = x + 10; } System.out.println("x is " + x); x is 30 x is 20 x is 10 x is 0
x is 10