CSE 110 Midterm Exam
Which of the following are NOT Java primitive data types? Choose all that apply. String float Random boolean char int Array Math double class
answer: String Random Array Math class
Given the following declaration: String s = "Apples and bananas are yummy"; Evaluate the expression: s.substring(1,3) [ ] "A" [ ] "Ap" [ ] "p" [ ] "pp" [ ] "Apple" [ ] "Apple and" [ ] none of these
answer: "pp"
Given the following declaration: String a ="abcdefghi"; Evaluate the expression: s.charAt(3) [ ] 'c' [ ] 'd' [ ] 'abc' [ ] 'abcd' [ ] 'e' [ ] none of these
answer: 'd'
Which of the following operators is NOT a relational operator? [ ] <= [ ] += [ ] != [ ] ==
answer: +=
Given the following declaration: String s = "Apples and bananas are yummy"; Evaluate the expression: s.indexOf("ann") [ ] -1 [ ] 0 [ ] 7 [ ] 8 [ ] 9 [ ] 10 [ ] 11 [.] 12 [ ] none of these
answer: -1
What is the output of the code snippet given below? int i = 0; while (i != 9) { System.out.println("" + i); i = i +2; } [ ] No output [ ] 0 2 4 6 8 [ ] 10 12 14 16 18... (infinite loop) [ ] 0 2 4 6 8 10 12 14... (infinite loop)
answer: 0 2 4 6 8 10 12 14... (infinite loop)
What is the output of this Java program? class Driver { public static void main(String[] args) { int a = 1; int b = 8; for (int c = 1; c < b; c++) { a = a + c; System.out.print(c); } System.out.print(a); } } [ ] 0123456729 [ ] 012345623 [ ] 0123456722 [ ] 012345629 [ ] 123456729 [ ] 123456722 [ ] none of these
answer: 123456729
Given the following declaration: String s = "green gardens"; Evaluate the expression: s.length() [ ] -1 [ ] 0 [ ] 10 [ ] 11 [ ] 12 [ ] 13 [ ] none of these
answer: 13
What is the output of this Java program? class Driver { public static void main(String[] args) { int a = 3; int b = 5; if (a > b || a * 2 > b) System.out.print(a); else System.out.print(b); } } [ ] 3 [ ] 5 [ ] 2 [ ] 8 [ ] none of these
answer: 3
What is the output of this Java program? class Driver { public static void main(String[] args) { int a = 4; int b = 3; if (a < b || a * 2 < b) System.out.print(a); System.out.print(b); } } [ ] 35 [ ] 53 [ ] 5 [ ] 3 [ ] none of these
answer: 3
Which of the following Java literals have the data type float? Choose all that apply. 123 3f 3.14 -3 'a' "3.0" '\n' 3.14f true false "true" 3.0
answer: 3f 3.14f
Assuming that the user enters 45 and 62 as inputs for n1 and n2, respectively, what is the output of the following code snippet? public static void main(String [] args) { System.out.print("Enter a number: "); Scanner in = new Scanner(System.in); String n1 = in.next(); System.out.print("Enter another number: "); String n2 = in.next(); String result = n1 + n2; System.out.print(result); } [ ] 46 [ ] 4662 [ ] 107 [ ] 4562
answer: 4562
What is the output of the following code snippet? System.out.print (4+4); System.out.print (12); [ ] 4 + 412 [ ] 4412 [ ] 812 [ ] 20
answer: 812
What is the value of the magicPowers variable after executing the following code snippet? String magicPowers = " "; int experienceLevel = 9; if (experienceLevel > 10) { magicPowers = magicPowers + "Golden sword "; } if (experienceLevel > 8) { magicPowers = magicPowers + "Shining lantern "; } if (experienceLevel > 2) { magicPowers = magicPowers + "Magic beans "; } A. Golden sword Shining lantern Magic beans B. Shining lantern Magic beans C. Magic beans D. An empty string
answer: B. Shining lantern Magic beans
What kind of error is it when your program has a syntax error? [ ] Compile-time error [ ] Logic error [ ] Run-time error [ ] Exception
answer: Compile-time error
What will be the output of the following code snippet? boolean token = false; while (token) { System.out.println("Hello"); } [ ] "Hello" will be displayed infinite times. [ ] No output because of compilation error. [ ] No output after successful compilation. [ ] "Hello" will be displayed only once.
answer: No output after successful compilation.
Which of the following correctly declares a variable type of String in Java? Choose all that apply. A. String a; B. String a = "0"; C. String a = new char("abc"); D. a = String(3); E. String[] a = "0"; F. String[] a = new String[3]; G. none of these
answer: String a; String a = "0";
Assuming that the user inputs a value of 25 for the price and 10 for the discount rate in the following code snippet, what is the output? public static void main(String[ ] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the price: "); double price = in.nextDouble(); System.out.print("Enter the discount rate: "); double discount = in.nextDouble(); System.out.println("The new price is: " + price - price * (discount/100)); } [ ] The new price is 25 [ ] The new price is 15 [ ] The new price is 22.5 [ ] The new price is 20.0
answer: The new price is 22.5
What are the two parts of an if statement? [ ] a condition and a body [ ] a check and an increment [ ] an increment and a body [ ] an increment and a return value
answer: a condition and a body
A loop inside another loop is called: [ ] a sentinel loop [ ] a nested loop [ ] a parallel loop [ ] a do/while loop
answer: a nested loop
Which one of the following code snippets compiles without errors and displays the output "Hello Good Day!" on the screen? a. System.out.print("Hello "); System.out.println("Good Day!"); b. System.out.print("Hello ); System.out.println("Good Day!"); c. System.out.print("Hello "); System.out.println("Good Day!") d. System.out.print("Hello "); System.out.println(Good Day!");
answer: a. System.out.print("Hello "); System.out.println("Good Day!");
Which of the following correctly declares a variable of type boolean in Java? Choose all that apply. A. boolean[] a = new boolean[3]; B. boolean a = new boolean(); C. a = boolean(true); D. boolean a = false; E. boolean[] a = 0; F. boolean a; G. none of these
answer: boolean a = false; boolean a;
What is the output of this Java program? class Driver { public static void main(String[] args) { int a = 3; int b = 1; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } } [ ] buz [ ] bar [ ] foo [ ] foobar [ ] foobuz [ ] barbuz [ ] none of these
answer: buz
What is the output of this Java program? class Driver { public static void main(String[] args) { int a = 2; int b = 9; while (a < b) { b = a + 1; b = b - 1; System.out.print("buz"); } System.out.print(a); } [ ] buzbuzbuzbuz4 [ ] buzbuzbuz6 [ ] buzbuzbuzbuz6 [ ] buzbuzbuz8 [ ] buzbuzbuz5 [ ] none of these
answer: buzbuzbuz5
When the following expression is evaluated, the result will be what Java data type? 3.0 + 5.0 [ ] int [ ] float [ ] double [ ] boolean [ ] char [ ] String [ ] none of these
answer: double
Given the following declarations: String s1 = "apple"; String s2 = "Apple"; Evaluate the expression: s2.equals(s1) [ ] true [ ] false [ ] none of these
answer: false
When the following expression is evaluated, the result will be what Java data type? 3 + 5 [ ] int [ ] float [ ] double [ ] boolean [ ] char [ ] String [ ] none of these
answer: int
Which of the following would be the best data type for a variable to store your age in years? [ ] int [ ] float [ ] double [ ] boolean [ ] char [ ] String [ ] none of these
answer: int
What are the values of num1 and num2 after this snippet executes? double num1 = 4.20; double num2 = num1 * 10 + 5.0; [ ] num1 = 4.20 and num2 = 63.0 [ ] num1 = 4.20 and num2 = 47.0 [ ] num1 = 42.0 and num2 = 42.0 [ ] num1 = 42.0 and num2 = 47.0
answer: num1 = 4.20 and num2 = 47.0
Given the following declarations: String s1 = "apple"; String s2 = "an apple"; Evaluate the expression: s1.compareTo(s2) < 0 [ ] true [ ] false [ ] none of these
answer: true
Which of the following Java literals have the data type boolean? Choose all that apply. 123 3f 3.14 -3 'a' "3.0" '\n' 3.14f true false "true" 3.0
answer: true false