INSY 4305
What is output by the following Java code segment? int temp = 200; if (temp > 90) { System.out.println("This porridge is too hot."); } if (temp < 70) { System.out.println("This porridge is too cold."); } if (temp == 80) { System.out.println("This porridge is just right!");} a. This porridge is too hot. b. This porridge is too cold. c. This porridge is just right! d.None of the above.
ANS: a. This porridge is too hot.
Overloaded methods always have the same _________. a.method name b.return type c.number of parameters d.order of the parameters
ANS: a. method name
The format specifier ________ is a placeholder for an int value. a.%n b.%d c.%int d.%s
ANS: b. %d
Which of the following for-loop headers results in equivalent numbers of iterations: A.for (int q = 1; q <= 100; q++) B.for (int q = 100; q >= 0; q--) C.for (int q = 99; q > 0; q -= 9) D.for (int q = 990; q > 0; q -= 90) a.A and B. b.C and D. c.A and B have equivalent iterations and C and D have equivalent iterations. d.None of the loops have equivalent iterations.
ANS: b. C and D.
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x <= 100); a. 5 b. 4 c. 3 d. 1
ANS: A
What will be returned from the following method? public static double methodA() { double a = 8.5 + 9.5; return a; } a. 18.0 b. 18 c. 8 d. 19
ANS: A
How many times will the following do-while loop be executed? int x = 11; do { x += 20; } while (x > 100); a. 0 b. 1 c. 5 d. 4
ANS: B
What is the value of z after the following code is executed? int x = 5, y = 28; float z; z = (float) (y / x); a. 5.6 b. 3.0 c. 5.0 d. 5.60
ANS: C
In the following code, what values could be read into number to terminate the while loop? Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int number = keyboard.nextInt(); while (number < 100 || number > 500) { System.out.print("Enter another number: "); number = keyboard.nextInt(); } a. Numbers less than 100 b. Numbers greater than 500 c. Numbers in the range 100 - 499 d. Numbers in the range 100 - 500
ANS: D
What will be the value of x after the following code is executed? int x, y = 15; x = y--; a. 14 b. 16 c. 0 d. 15
ANS: D
What would be displayed as a result of executing the following code? final int x = 22, y = 4; y += x; System.out.println("x = " + x + ", y = " + y) a. x = 22, y = 26 c. x = 22, y = 88 b. x = 22, y = 4 d. Nothing. There is an error in the code.
ANS: D.
Counter-controlled iteration is also known as: a. Definite iteration b. Indefinite iteration c. Multiple-iteration structure d.Double-iteration structure
ANS: a. Definite iteration
The empty statement is denoted by what symbol? a. Semicolon ; b. Parentheses () c. Braces {} d.Colon :
ANS: a. Semicolon ;
What is output by the following Java code segment? int temp = 180; while (temp != 80) { if (temp > 90) { System.out.print("This porridge is too hot! "); // cool down temp = temp - (temp > 150 ? 100 : 20); } else { if (temp < 70) { System.out.print("This porridge is too cold! "); // warm up temp = temp + (temp < 50 ? 30 : 20); } } } if (temp == 80) { System.out.println("This porridge is just right!"); } a. This porridge is too cold! This porridge is just right! b. This porridge is too hot! This porridge is just right! c. This porridge is just right! d.None of the above.
ANS: b. This porridge is too hot! This porridge is just right!
Which of the following promotions of primitive types is not allowed to occur? a.char to int. b.double to float. c.int to double. d.short to long.
ANS: b. double to float.
Which expression is equivalent to if (!(grade == sentinelValue))? a.if (grade !== sentinelValue) b.if (grade != sentinelValue) c.! if (grade == sentinelValue) d.! if (grade !== sentinelValue)
ANS: b. if (grade != sentinelValue)
Which of the following is equivalent to this code segment? int total = 0; for (int i = 0; i <= 20; i += 2) { total += i; } a.int total = 0;for (int i = 20; i < 0; i += 1) { total += i;} b.int total = 0;for (int i = 0; i <= 20; total += i, i += 2) {} c.int total = 0;for (int i = 0, i <= 20, total += i; i += 2) {} d.int total = 0;for (int i = 2; i < 20; total += i, i += 2) {}
ANS: b. int total = 0;for (int i = 0; i <= 20; total += i, i += 2) {
Which statement below could be used to simulate the outputs of tossing a quarter to get heads or tails? Suppose randomNumbers is a SecureRandom object. a.randomNumbers.nextInt(7); b.randomNumbers.nextInt(2); c.randomNumbers.nextInt(1); d.randomNumbers.nextInt(25);
ANS: b. randomNumbers.nextInt(2);
Consider the following Java statements: int x = 9; double y = 5.3; result = calculateValue(x, y); Which of the following statements is false? a.A method is called with its name and parentheses. b.x and y are parameters. c.Copies of x and y are passed to the method calculateValue. d.x and y are arguments.
ANS: b. x and y are parameters.
A Java class can have which of the following methods? A.void foo(int a) B.void foo(int a, int b) C.void foo(double a) D.void foo(double a, double b) E.void foo(int b) a.All of the above. b.A, B, D, E. c.A, B, C, D. d.A, C, D, E.
ANS: c. A, B, C, D.
Stacks are known as ________ data structures. a.FIFO. b.FILO. c.LIFO. d.LILO.
ANS: c. LIFO.
Which is a correct static method call of Math class method sqrt? a.sqrt(900); b.math.sqrt(900); c.Math.sqrt(900); d.Math math = new Math(); math.sqrt(900);
ANS: c. Math.sqrt(900);
Which of the following statements about a do...while iteration statement is true? a.The body of a do...while loop is executed only if the terminating condition is true. b.The body of a do...while loop is executed only once. c.The body of a do...while loop is always executed at least once. d.None of the above.
ANS: c. The body of a do...while loop is always executed at least once.
Which of the following code segments does not increment val by 3: a. val += 3; b. val = val + 1;val = val + 1;val = val + 1; c. c = 3;val = val + (c == 3 ? 2 : 3); d.All of the above increment val by 3.
ANS: c. c = 3; val = val + (c == 3 ? 2 : 3);
Which formatting flag indicates that the floating-point values should be output with a thousands separator? a.plus (+). b.minus (-). c.comma (,). d.period (.).
ANS: c. comma (,).
Declaring main as static allows the JVM to invoke main ________. a.without knowing the name of the class in which main is declared. b.by creating an object of the class in which main is declared. c.without creating an instance of the class in which main is declared. d.None of the above.
ANS: c. without creating an instance of the class in which main is declared.
In a class containing methods with the same name, the methods are distinguished by ________. a.Number of arguments b.Types of arguments c.Return type d.(a) and (b) e.(b) and (c)
ANS: d. (a) and (b).
How many times is the body of the loop below executed? int counter = 1; while (counter > 20) { // body of loop counter = counter - 1; } a. 19. b. 20. c. 21. d.0.
ANS: d. 0.
Consider the classes below: public class TestA { public static void main(String[] args) { int x = 2; int y = 20 int counter = 0; for (int j = y % x; j < 100; j += (y / x)) { counter++; } } } public class TestB { public static void main(String[] args) { int counter = 0; for (int j = 10; j > 0; --j) { ++counter; } } } Which of the following statements is true? a.The value of counter will be different at the end of each for loop for each class. b.The value of j will be the same for each loop for all iterations c.Both (a) and (b) are true. d.Neither (a) nor (b) is true.
ANS: d. Neither (a) nor (b) is true.
For the two code segments below: Segment A int q = 5; switch(q) { case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); case 4: System.out.println(4); case 5: System.out.println(5); default: System.out.println("default"); } Segment B q = 4; switch(q) { case 1: System.out.println(1); case 2: System.out.println(2); case 3: System.out.println(3); case 4: System.out.println(4); case 5: System.out.println(5); default: System.out.println("default"); } Which of the following statements is true? a.The output for Segment A is: default b.The output for Segment B is: 4 c.The output for Segment B is: 45default d.The output for Segment A is: 5default
ANS: d. The output for Segment A is: 5 default
Any field declared with keyword ________ is constant. a.static b.const c.constant d.Final
ANS: d. final
A well-designed method ________. a.performs multiple unrelated tasks b.repeats code found in other methods c.contains thousands of lines of code d.performs a single, well-defined task
ANS: d. performs a single, well-defined task.
What is the output of the following statement? System.out.printf("%s", "I love ", "Java");
I love
Which of the following methods is not in the Math class? a.ceil b.abs c.parseInt d.Log
Which of the following methods is not in the Math class? a.ceil b.abs c.parseInt d.Log