APCSA S1 EXAM

Ace your homework & exams now with Quizwiz!

Assume that x and y have been defined and initialized as int values. The expression: !(!(x < y) || (y != 5)) is equivalent to which of the following? a. (x < y) && (y == 5) b. (x < y) && (y != 5) c. (x >= y) && (y == 5) d. (x < y) || (y == 5) e. (x >= y) || (y != 5)

a. (x < y) && (y == 5)

What is stored in the variable s after the following code is executed? int s = Math.sqrt(26); a. Nothing: an error occurs due to a possible loss of precision b. 5.09901951359278 c. 6 d. 5 e. 5.0

a. Nothing: an error occurs due to a possible loss of precision

Consider the following code: double x = -97.6; System.out.println(Math.abs(x)); What is the output? a. 98 b. 97.6 c. -98 d. -97.6 e. -96

b. 97.6

Which of the following is used to indicate a new line? a. \' b. \n c. \t d. \" e. \\

b. \n

Consider the class below: public class Stuff { public Stuff() { System.out.print("one"); } public Stuff(int x) { System.out.print("two"); } public Stuff(double x) { System.out.print("three"); } } What is output by the following? Stuff something = new Stuff(8); a. one b. two c. three d. twothree e. Nothing is printed

b. two

Consider the following code: int c = 3 - 37 % 2; System.out.println(c); What is output? a. -18 b. 0 c. 2 d. 3 e. 15.5

c. 2

What is printed by the following code segment? int x - 24601; System.out.println(x.substring(3, 4)); a. 6 b. 60 c. Nothing is printed, an error occurs. d. 0 e. 01

c. Nothing is printed, an error occurs.

What is output to the screen by the following code? int f = 0; while ( f < 8 ) { f++; System.out.print(f % 3 + " " ); } a. 2 0 1 2 0 1 2 0 b. 1 2 0 1 2 0 1 2 0 c. 0 1 2 0 1 2 d. 1 2 0 1 2 0 1 2 e. 2 0 1 2 0 1 2

d. 1 2 0 1 2 0 1 2

Which of the following would properly create A and B as integer variables? a. int A; B; b. A int; B int; c. int A int B; d. int A; int B; e. int A B;

d. in A; int B;

What is the value of w after executing this segment of code? int w = 18; w += 18; a. 324 b. 18 c. 0 d. Nothing, there is an error. e. 36

3. 36

Assume that x and y are boolean variables and have been properly initialized.(x && y) | | !(x | | y ) Which of the following best describes the result of evaluating the expression above? a. True only when x is true and y is true b. Always false c. Always true d. True only when x and y have different values e. True only when x and y have the same value

e. True only when x and y have the same value

What is (6 % 2) * 7? a. 1 b. 0 c. 21 d. 7 e. 14

b. 0

Consider the following code: int x = 5; int y = 8; System.out.println(x + y); What is output when this code is executed? a. 85 b. 13 c. 1 d. 58 e. 5 8

b. 13

Consider the following code: int x = -3; x--; System.out.println(x); What is output? a. 4 b. -2 c. -3 d. -4 e. 2

d. -4

The following truth table matches which boolean condition? a b ? 1 1 1 1 0 1 0 1 0 0 0 1 a. A && ( A && B ) b. !A && ( A || B ) c. A && ( A || B ) d. A || ( !A && !B ) e. A || ( A || B )

d. A || ( !A && !B )

Which of the following is NOT a primitive data type? a. boolean b. double c. int d. String e. All of the above are primitive data types

d. String

Which of the following correctly stores the word umbrella in a variable called stuff? a. String "stuff" = "umbrella"; b. String stuff = umbrella; c. String umbrella = stuff; d. String stuff = "umbrella"; e. String umbrella = "stuff";

d. String stuff = "umbrella";

Consider the following variable declaration: double y = 52; Does a cast need to be added so this code will compile and run successfully? ______. If so, what should be typed for this cast? _______. a. yes, (int) b. yes, (double) c. yes, (String) d. no, nothing e. yes, (decimal)

d. no, nothing

Consider the following expression. Assume that x and y are properly initialized boolean variables. (x && y) | | !(x | | y ) The result of evaluating the expression is best described as: a. always true b. always false c. true only when x is true and y is true d. true only when x and y have the same values e. true only when x and y have different values

d. true only when x and y have the same values

Which of the following variable declarations conforms to Java's naming conventions? a. the answer b. test.1 c. 3average d. boolean e. ans

e. ans

!( x < y && w == z) is the same as which boolean expression? a. x <= y || w != z b. x <= y && w == z c. x < y && w != z d. x <= y && w != z e. x >= y || w != z

e. x >= y || w != z

What is output by the following code? int a = 91; System.out.println(a / 2); a. 44 b. 45.5 c. 46 d. 46.5 e. 45

e. 45

Consider the following output. 1 2 3 4 1 2 3 1 2 1 Which of the following code segments will produce this output? a. for (int j = 4; j > 0; j--) { for (int k = 1; k <= j; k++) { System.out.print(k + " "); } System.out.println(); } b. for (int j = 4; j > 0; j--) { for (int k = 1; k <= j; k++) { System.out.print(j + " "); } System.out.println(); } c. for (int j = 1; j <= 4; j++) { for (int k = 1; k <= j; k++) { System.out.print(k + " "); } System.out.println(); } d. for (int j = 1; j <= 4; j++) { for (int k = j; k > 0; k--) { System.out.print(k + " "); } System.out.println(); } e. for (int j = 4; j > 0; j--) { for (int k = j; k > 0; k--) { System.out.print(k + " "); } System.out.println(); }

a. for (int j = 4; j > 0; j--) { for (int k = 1; k <= j; k++) { System.out.print(k + " "); } System.out.println(); }

Which of the following code segments prints the number of times the code substring "sh" appears in the String str? a. int count = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 2).equals("gr")) { count++; } } System.out.println(count); b. int count = 0; for (int i = 0; i < str.length() - 1; i += 2) { if (str.substring(i, i + 2).equals("gr")) { count++; } } System.out.println(count); c. int count = 0; for (int i = 0; i < str.length(); i += 2) { if (str.substring(i, i + 2).equals("gr")) { count++; } } System.out.println(count); d. count = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 1).equals("gr")) { count++; } } System.out.println(count); e. int count = 0; for (int i = 0; i < str.length(); i++) { if (str.substring(i, i + 2).equals("gr")) { count++; } } System.out.println(count);

a. int count = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 2).equals("gr")) { count++; } } System.out.println(count);

Consider the following code segment. for (int n = 1; n < 20; n += 8) { for (int k = n; k < n + 5; k++) { if (k % 3 == 0) { System.out.print(k + " "); } } System.out.println(); } What will be printed as a result of executing the code segment? a. 3 6 9 12 18 21 b. 1 2 3 4 5 9 10 11 12 13 c. 3 9 12 18 21 d. 3 9 12 18 e. 3 6 9 12 18

c. 3 9 12 18 21

What is output by the following code? String str1 = "fast" ; String str2 = "purple" ; int i = 0 ; int j = str2.length() - 1 ; while (i < str1.length() && j >= 0 ) { System.out.print(str1. substring(i, i + 1 )) ; System.out.print(str2. substring(j, j + 1 )) ; i++ ; j--; } a. fealsptrup b. flapsrau c. fpausrtp d. fealsptr e. eflapsrt

d. fealsptr

Consider the class Point: public class Point { private double x; private double y; public Point() { this(0, 0); } public Point(double a, double b) { /* missing code */ } public double getX() { return x; } public double getY() { return y; } } The default constructor sets x and y to (0, 0) by calling the second constructor. What could be used to replace /* missing code */ so that this works as intended? a. x = a; y = b; b. a = 0; b = 0;, c. this(x, y); d. a = x; b = y;, e. this(0, 0);

a. x = a; y = b;

Which of the following correctly gives random numbers between -10 and 10 inclusive? a. int n = (int) (Math.random() * 21) - 10; b. int n = (int) (Math.random() * 20) - 10; c. int n = (int) (Math.random() * 11) - 20; d. int n = (int) (Math.random() * 10) - 21; e. int n = (int) (Math.random() * 10) - 20;

a. int n = (int) (Math.random() * 21) - 10;

The class Lightbulb contains a non-static void method with no parameters named turnOn . Suppose a Lightbulb object loungeLamp has been declared as follows: Lightbulb loungeLamp = new Lightbulb(); Which of the following correctly calls the method turnOn? a. loungeLamp.turnOn(); b. loungeLamp.turnOn(Lightbulb); c. Lightbulb.turnOn(loungeLamp); d. turnOn(loungeLamp); e. Lightbulb.turnOn();

a. loungeLamp.turnOn();

What is output by the following code? int val = -2; val++; val--; val++; val++; val++; val--; val++; System.out.println(val); a. 2 b. 1 c. 5 d. 0 e. 3

b. 1

Consider the following code segment. int x = 2 ; while (x < 10 || (x % 5) != 0) { x += 3 ; } System.out.println(x) ; What is printed as a result of executing the code segment? a. 5 b. 20 c. 11 d. Nothing is printed, due to an infinite loop e. 15

b. 20

Which of the following statements will return a random even number between 6 and 14 inclusive (including 6 and 14)? a. 4 + 2 * (int) (5 * Math.random()) b. 6 + 2 * (int) (5 * Math.random()) c. 6 + 2 * (int) (4 * Math.random()) d. 6 + (int) (8*Math.random()) e. (int) (Math.random()*14) - 6

b. 6 + 2 * (int) (5 * Math.random())

Consider the following code: public static int number(int a) { int x = 1; while (x < a) { x = -2 * x; a++; } return x; } Which of the following is true about the behavior of method number? a. The method will always return the same number, regardless of the argument passed to it. b. The method will always return a number and never result in an infinite loop. c. The method will result in an infinite loop for argument values larger than 1000000, but when a in smaller than that, the loop does return a number d. The method will result in an infinite loop for arguments that are negative, but when a is originally positive, the loop does end and a value is returned e. The method will never return a number as it always results in an infinite loop.

b. The method will always return a number and never result in an infinite loop.

What is output by the following code segment? Assume a Time class exists. Time appointment1 = new Time(5, 45); Time appointment2 = new Time(5, 45); if (appointment1 == appointment2) { System.out.print("equal "); } else { System.out.print("not equal "); } a. equal b. not equal c. equal not equal d. not equal equal e. Nothing, there is an error

b. not equal

Consider the following code: String w = "Rapunzel" ; for (int i = 0 ; i < w.length(); i++) { if (i % 4 != 2) { System.out.print(w.substring(i, i + 1) + " ") ; } } What is output? a. R a p u n z e l b. R a p u n z e l c. R a u n z l d. u l e. R a p u n z e l

c. R a u n z l

Consider the following declaration Widget thing = new Widget(); Which of the following best describes the situation? a. Widget is a primitive value of the thing type b. Widget is an object of the thing class type c. thing is an object of the Widget class type d. Widget and thing are both classes e. thing is a primitive value of the Widget type

c. thing is an object of the Widget class type

What is output to the screen by the following code? int c = 2; while (c < 6) { System.out.print((int)Math.pow(-1, c) + " "); c++; } a. -1 1 -1 1 -1 1 -1 b. -1 1 -1 1 -1 1 c. 1 1 1 1 1 1 d. 1 -1 1 -1 e. -1 -1 -1 -1 -1 -1

d. 1 -1 1 -1

Given the variables, which boolean condition is true? int a = 5; int b = 5; a. a < b && a == b b. a > b || a != b c. a < b && a != b d. b >= a && a >= b e. None of the above

d. b >= a && a >= b

Consider the class Point: public class Point { private int x; private int y; public Point() { /* missing code */ } public Point(int a, int b) { x = a; y = b; } public int getX() { return x; } public int getY() { return y; } // ... other methods not shown } The default constructor is meant to set x and y to 0 and 0 by calling the second constructor. What could be used to replace /* missing code */ so that this works as intended? a. x = a; y = b; b. a = x; b = y; c. this(0, 0); d. this(x, y); e. this(a, b);

e. this(a, b);

When defining a class, it is a best practice to declare ____ as private. a. instance variables b. constructors c. nothing should be private d. accessors e. mutators

a. instance variables

Consider the class below: public class A { public A() { System.out.print("one"); } public A(int x) { System.out.print("two"); } } What is output by the following? A a = new A(); a. one b. nothing c. onetwo d. two e. twoone

a. one

You have written a class called House . In a second program you have the line: House.printType(); Which of the following must be true about the method printType() for the method call to work? a. printType() must be declared static. b. Nothing, this method call is not legal and will cause an error. c. printType() must be declared private. d. printType() must return a value. e. printType() must be void

a. printType() must be declared static.

Consider the following code: String a = new String("surprise"); String b = new String("surprise"); if (a == a) { System.out.print("yes "); } else { System.out.print("no "); } if (a == b) { System.out.print("yes "); } else { System.out.print("no "); } if (a.equals(b)) { System.out.print("yes"); } else { System.out.print("no"); } What will be printed when this code segment is executed? a. yes no yes b. no no yes c. yes yes yes d. no yes yes e. yes no no

a. yes no yes

Which of the following describes the return type and parameters of the Scanner method nextLine? a. Return type: int. Parameters: multiple String and int parameters b. Return type: String. Parameters: none c. Return type: void. Parameters: one String parameter d. Return type: void. Parameters: none e. Return type: String. Parameters: one String parameter

b. Return type: String. Parameters: none

The image below shows the Javadoc for all constructors of a class named Shoe. Which of the following constructor calls will NOT compile correctly? a. Shoe C = new Shoe(11, false); b. Shoe E = new Shoe(7, "Left"); c. Shoe B = new Shoe(8); d. Shoe A = new Shoe(); e. Shoe D = new Shoe(13 / 2, true);

b. Shoe E = new Shoe(7, "Left");

Which of the following could be the signature of a constructor from a class named CustomClass ? a. newCustomClass(); b. CustomClassConstructor(); c. CustomClass(int num); d. constructor(double x, double y); e. All of the above could be signatures for constructors for this class

c. CustomClass(int num);

What is output by the following code segment? Integer a = new Integer(12); Integer b = new Integer(22); System.out.println(a.compareTo(b)); a. -1 b. 0 c. 1 d. false e. true

a. -1

Choose which three values should replace the blanks in the for loop so that it loops through the numbers: 10 8 6 4 2.Note that the choices will fill in the three blanks in the order which they appear. for (int i = ___; i ____; i _____) { System.out.print(i + " "); } a. 10, > 0, -= 2 b. 10, <= 2, -= 2 c. 10, >= 2, -- d. 10, >= 0, -= 2 e. 10, > 2, -= 2

a. 10, > 0, -= 2

Consider the following code. int j = (int) (3 * Math.random() + 5); Which of the following could be values for j after this code segment is run? Choose all options that apply. a. 6 b. 5 c. 6.4 d. 5.2 e. 8 f. 7

a. 6 b. 5 f. 7

Which of the following is true about designing classes. a. There is no need to create a default constructor, because by Java automatically creates one, which declares all class variables as either Null, 0, 0.0 or false, depending on the type of variable. b. There is no need to create an equals() method, because by default one is provided. c. All class variables should be declared as public. d. There is no need to create a toString method, because by default Java knows what to print and in the correct format e. All methods should be declared private.

a. There is no need to create a default constructor, because by Java automatically creates one, which declares all class variables as either Null, 0, 0.0 or false, depending on the type of variable.

Assume that x and y are boolean variables and have been properly initialized. !(x | | y ) | | (x | | y ) The result of evaluating the expression above is best described as: a. always true b. always false c. true only when x is true and y is true d. true only when x and y have the same value e. true only when x and y have different values

a. always true

Which of the following data types would be most appropriate to use when recording whether a switch is in the "on" or "off" position? a. boolean b. int c. double d. String e. None of the choices above could be used

a. boolean

For which of the following would modular division be LEAST likely to be useful? a. converting decimals to whole numbers b. testing whether numbers are even or odd c. money calculations using dollars and cents d. time calculations using days and weeks e. identifying the digits of an integer

a. converting decimals to whole numbers

To test if a grade (represented by the variable g) is a C (between 70 and 79 inclusive) you would use the following if statement: if (g __ 70 __ g __ 79) What three sets of symbols correctly fill the blanks in the order they appear? a. <, ||, > b. >=, &&, <= c. >=, ||, <= d. >, ||, < e. <, &&, >

b. >=, &&, <=

The following if statement tests the rainfall in New York's Central Park during the months of June, July and August. It uses the double variables low, rain and high. if (low <= rain && rain <= high) { System.out.println("Rainfall amount is normal."); } else { System.out.println("Rainfall amount is abnormal."); } It could be replaced with: 1. if (rain >= low) { if (rain <= high) { System.out.println("Rainfall amount is normal."); } } else { System.out.println("Rainfall amount is abnormal."); } 2. if (rain >= low) { if (rain <= high) { System.out.println("Rainfall amount is normal."); } else { System.out.println("Rainfall amount is abnormal."); } } else { System.out.println("Rainfall amount is abnormal."); } 3. if (rain >= low) { System.out.println("Rainfall amount is normal."); } else if (rain <= high) { System.out.println("Rainfall amount is normal."); } else { System.out.println("Rainfall amount is abnormal."); } a. I only b. II only c. III only d. II and III only e. I, II and III

b. II only

Consider the following class. public class MyClass { public MyClass(int arg1) { /* implementation not shown */ } public MyClass(String arg1) { /* implementation not shown */ } public MyClass(String arg1, int arg2) { /* implementation not shown */ } } Which of the following additional constructors can be added to MyClass without causing a compile-time error? 1. public MyClass(String alt1) { /* implementation not shown */ } 2. public MyClass(String alt1, int alt2) { /* implementation not shown */ } 3. public MyClass(int arg1, String arg2) { /* implementation not shown */ } a. I and II only b. III only c. Ii and III only d. I, II and III e. None of I, II or III

b. III only

Consider the following class. public class myClass { public myClass(int arg1) { /* implementation not shown */ } public myClass(String arg1) { /* implementation not shown */ } public myClass(String arg1, int arg2) { /* implementation not shown */ } } Which of the following additional constructors can be added to myClass without causing a compile-time error? 1. public myClass(String alt1) { /* implementation not shown */ } 2. public myClass(String alt1, int alt2) { /* implementation not shown */ } 3. public myClass(int alt1) { /* implementation not shown */ } a. I, II and III b. None of these additional constructors can be added without causing an error c. I only d. II only e. III only

b. None of these additional constructors can be added without causing an error

Assuming that str is a correctly initialized String, which of the following best describes what the algorithm below does? int index = str.indexOf(" "); while (index != -1) { System.out.print(str.substring(index - 1, index)); str = str.substring(index + 1); index = str.indexOf(" "); } a. Prints characters from str in an infinite loop. b. Prints the character before each space in str. c. Prints the number of times a space appears in str. d. Prints the character after each space in str. e. Prints each character in str followed by a space.

b. Prints the character before each space in str.

The following truth table matches which boolean condition? a b ? 1 1 1 1 0 1 0 1 0 0 0 0 a. !A && ( A || !B ) b. A || ( !A && !B ) c. A && ( A || B ) d. A && ( A && B ) e. A || ( A || B )

c. A && ( A || B )

Look at the following code - assume that Game is a class that creates an object that represents the progress of a player within a video game. By default, the value stored is 1. Also, assume that changeLevel() is a void method that will update the progress by adding 1 to the current value stored. Game progress1 = new Game(); Game progress2 = progress1; progress1.changeLevel(); What is true about progress1 and progress2 ? a. The value of the level stored in progress1 is now 2 and the value stored in progress2 is 1. b. progress1 and progress2 point to the same object, so the level of progress1 and progress2 are both now 2. c. An error occurs, because when progress1 is stored to progress2, progress1 no longer exists so changeLevel() can no longer be used on progress1. d. The level of progress1 is changed to 2 by the changeLevel() method, but since it is not simultaneously changed for progress2, the level is not updated completely. Therefore the level of progress1 and progress2 are both 1. e. An error occurs because progress2 is not properly created. You cannot set one object equal to another object.

b. progress1 and progress2 point to the same object, so the level of progress1 and progress2 are both now 2.

Consider the complete class definition below: public class Die { public static void rollIt() { /* Missing Code */ } } Which of the following is the correct way to call the function rollIt() from another class? a. Die d = new Die(); d.rollIt(); b. Die d = new Die(); rollIt(d); c. Die.rollIt(); d. rollIt(); e. None of these are correct.

c. Die.rollIt();

Consider the following incomplete code segment. int x = /* missing code */ ; if (x == 20) { System.out.println("yes"); } else { System.out.println("no"); } Consider the following potential replacements for /* missing code */ . Which of these replacements will cause the code segment to always print no? a. (int)(Math.random() * 10) + 20 b. (int)(Math.random() * 10) + 15 c. (int)(Math.random() * 10) + 10 d. (int)(Math.random()) + 20 e. (int)(Math.random() + 20)

c. (int)(Math.random() * 10) + 10

Consider the following code which uses the String variable s : if (s.toLowerCase().equals(s.toUpperCase())) { System.out.println("*"); } What does the code do? a. Prints "*" if the String s contains only upper-case letters. b. Prints "*" if the String s contains only lower-case letters. c. Prints "*" if the String s contains only non-letter characters. d. Prints "*" if the String s contains only letter characters (no digits or symbols). e. Prints "*" if the String s contains a mixture of upper and lower-case letters.

c. Prints "*" if the String s contains only non-letter characters.

Which of the following will print the ones column of an integer stored in x? a. System.out.print(10 % x); b. System.out.print(x % 1); c. System.out.print(x % 10); d. System.out.print(x / 10); e. None of the above

c. System.out.print(x % 10);

Consider the following code: String str = "Computer Science" ; Which of the following statements correctly prints the first character in the String str? a. System.out.println(str.substring(1, 1)); b. System.out.println(str.substring(0)); c. System.out.println(str.substring(0, 1)); d. System.out.println(str.substring(0, 0)); e. System.out.println(str.substring(1, 2));

c. System.out.println(str.substring(0, 1));

Consider the following method: public static int magic(int a) { int x = 9; while (x < 20) { x = 2 * x; x = x - a; if (x < 0) { x = 0; } } return x; } Which of the following is true about the behavior of method magic? a. The method will result in an infinite loop for some arguments, but will return a number for all arguments which are sufficiently large. b. The method will never return a number as it always results in an infinite loop. c. The method will result in an infinite loop for some arguments, but will return a number for all arguments which are sufficiently small. d. The method will always return a number and never result in an infinite loop. e. The method will always return the same number, regardless of the argument passed to it.

c. The method will result in an infinite loop for some arguments, but will return a number for all arguments which are sufficiently small.

Consider the following methods. public int manipulate(boolean b, int n) { if (!b) { return n + 1; } else { return n - 1; } } public void tester() { boolean a = true; int x = 17; x = manipulate(a, x); System.out.println("a = " + a + ", x = " + x); } What is printed by the call tester() ? a. a = true, x = 17 b. a = false, x = 18 c. a = true, x = 16 d. a = true, x = 18 e. a = false, x = 16

c. a = true, x = 16

Consider the following method minimum, which is intended to return the smallest of three integers. public static int minimum(int a, int b, int c) { if ((a < b) && (a < c)) { return a; } else if (/* missing condition */) { return b; } else { return c; } } Which of the following should replace /* missing condition */ so that minimum works as intended? a. (b < c) && (c < a) b. (a < c) && (b < c) c. b < c d. (c < a) && (c < b) e. (b < a) && (b < c)

c. b < c

Consider the code: if (a < b && c != d) Which of the following is an example of short circuit evaluation? a. if a < b is true it evaluates c != d b. if c != d is true it doesn't evaluate a < b c. if a < b is false it doesn't evaluate c != d d. if c != d is false it evaluates a < b e. if a < b is true it doesn't evaluate c != d

c. if a < b is false it doesn't evaluate c != d

Correct the following code so that q stores the nearest integer below 82.3847. int q = 82.3847; a. int q = double (82.3847); b. int q = int (82.3847); c. int q = (int) 82.3847; d. int q = (double) 82.3849; e. No changes, the code is fine.

c. int q = (int) 82.3847;

You have written a class called Tree . In a second program you have the line: Tree.printTemperature(); Which of the following must be true about the method printTemperature() for the method call to work? a. printTemperature() must return a value. b. printTemperature() must be declared private. c. printTemperature() must be declared static. d. Nothing, this method call is not legal and will cause an error. e. printTemperature() must be void.

c. printTemperature() must be declared static.

Consider the code: if (y == 0 || x * y > 10) Which of the following is an example of short circuit evaluation? a. if x*y > 10 is false it evaluates y == 0 b. if x*y > 10 is false it doesn't evaluate y == 0 c. if y == 0 is false it doesn't evaluate x*y > 10 d. if y == 0 is true it doesn't evaluate x*y > 10 e. if y == 0 is false it evaluates x*y > 10

d. if y == 0 is true it doesn't evaluate x*y > 10

When defining a class, which of the following should NOT be declared as public? a. constructors b. accessors c. mutators d. variables e. Nothing should be private

d. variables

What is output by the following code? int a = 0; for (int i = 1; i < 5; i++) { for (int j = 1; j < 4; j++) { a++; } } System.out.println(a); a. 20 b. 16 c. 9 d. 12 e. 0 f. 7 g. 15

d. 12

What is output by the following code? int x = 9 ; if (x / 2 <= 4) { x++ ; } if (x / 2 >= 5) { x += 2 ; } System.out.println(x) ; a. 9 b. 10 c. 11 d. 12 e. There is an error - an odd int cannot be divided by 2

d. 12

Consider the following code segment. int sum = 0; for (int a = 1; a < 4; a++) { for (int b = 1; b <= a; b++) { sum += a; } } System.out.println(sum); What is printed as a result of executing the code segment? a. 6 b. 8 c. 10 d. 14 e. 30

d. 14

Consider the following code: String q = "power" ; String r = "brown" ; System.out.println(q.indexOf(r.substring(3, 4))) ; What is the output? a. o b. w c. 1 d. 2 e. 3

d. 2

Which of the following is not true about designing classes. a. The constructor must be declared as public. b. In order for class information to be printed instead of a memory address, a toString method should be declared. c. To test for equality the programmer should define an equals method. d. All methods should be declared private. e. All class variables should be declared as private.

d. All methods should be declared private.

Consider the following code segment: int a = 20; while ( /* missing condition */ ) { System.out.println("a = " + a); a = a - 3; } For which of the following replacements for /* missing condition */ will the segment result in an infinite loop (the code will continue running and never stop)? I. a <= 20 II. a <= 12 III. a != 10 a. I only b. III only c. I and II only d. I and III only e. I, II and III

d. I and III only

Consider the following code which uses the int variable num : if (num < 0) { System.out.println("One"); } else if (num < 5) { System.out.println("Two"); } else { System.out.println("Three"); } Which of the following will always produce the same result as the code above, regardless of the value of num? 1. if (num < 0) { System.out.println("One"); } else if (num >= 5) { System.out.println("Three"); } else { System.out.println("Two"); } 2. if (num < 5) { System.out.println("Two"); } else if (num < 0) { System.out.println("One"); } else { System.out.println("Three"); } 3. if (num >= 5) { System.out.println("Three"); } else if (num >= 0) { System.out.println("Two"); } else { System.out.println("One"); } a. I only b. III only c. I and II only d. I and III only e. I, II and III

d. I and III only

Which of the following is a call to a static method? a. string1.length() b. a.intValue() c. new Double() d. Math.random() e. Integer.MAX_VALUE

d. Math.random()

Assuming that scan is a properly initialized Scanner variable, which of the following correctly inputs a String? a. String val = nextDouble(); b. String val = scan.nextValue(); c. String val = scan.nextInt(); d. String val = scan.nextLine(); e. String val = scan.nextDouble();

d. String val = scan.nextLine();

What is the output? System.out.println("The answer is: " + Math.pow(2, 3)); a. The answer is: 6 b. The answer is: 8 c. The answer is: 1 d. The answer is: 8.0 e. The answer is: 6.0

d. The answer is: 8.0

When might you encounter a problem with integer overflow? a. When trying to divide one integer by another using ints when the answer should be a decimal value b. When trying to store a decimal number in an int variable c. When trying to store an integer value as a double d. When trying to store an integer which is too big to be stored in an int variable e. When trying to print an int in the middle of a String

d. When trying to store an integer which is too big to be stored in an int variable

Consider the following methods. public void manipulate(boolean b, int n) { b = !b; n = n % 5; } public void tester() { boolean a = true; int x = 17; manipulate(a, x); System.out.println("a = " + a + ", x = " + x); } What is printed by the call tester() ? a. a = false, x = 3 b. a = false, x = 2 c. a = false, x = 17 d. a = true, x = 17 e. a = true, x = 2

d. a = true, x = 17

Consider the following code segment for (int n = 2; n < 5; n++) { int k; for (k = n; k > n / 2; k--) { System.out.print("a"); } for (k = n / 2; k > 0; k--) { System.out.print("b"); } System.out.print(" "); } a. ab abb aabb b. ab aabb aabb c. ba bba bbaa d. ab aab aabb e. ba baa bbaa

d. ab aab aabb

There are two integer variables in our program, minutes and hours, which represents time. If, in the program, we increase the number of minutes by one, which of the following lines of code will correctly update hour and minutes? a. hours = hours + minutes / 60; b. hours = hours + minutes % 60; minutes = minutes / 60; c. minutes = minutes + hours % 60; d. hours = hours + minutes / 60; minutes = minutes % 60; e. minutes = minutes % 60;

d. hours = hours + minutes / 60; minutes = minutes % 60;

Consider the following code segment? String str = "example"; String result = ""; for (int i = 0; i < str.length(); i++) { int index = (i + 2) % str.length(); result += str.substring(index, index + 1); } What is stored by the string result after this code segment has been executed? a. Nothing, an IndexOutOfBoundsException is thrown b. "ample" c. "example" d. "leexamp" e. "ampleex"

e. "ampleex"

Consider the following code: int x = 9; int y = 6; System.out.println((x * y) / x); What is output? a. 9 b. 36 c. 81 d. 54 e. 6

e. 6

What is output? int x = 10; System.out.println("Answer: " + x + 5); a. None of the above b. Answer: x10 c. Answer: x5 d. Answer: 15 e. Answer: 105

e. Answer: 105

A weather app records the current temperature to the nearest degree Fahrenheit using an int variable temp. The app also uses a String variable tempAdj to store an adjective based on the value of temp which will be used in a short written summary of the current weather. The values of tempAdj are determined as follows: Value of temp Value of tempAdj 80 or greater "Hot" From 60 to 79 inclusive "Warm" From 40 to 59 inclusive "Cold" 39 or less "Very Cold" Which of the following code segments will correctly set the value of tempAdj for any given value of temp? 1. tempAdj = "Hot"; if (temp < 80) { tempAdj = "Warm"; } if (temp < 60) { tempAdj = "Cold"; } if (temp < 40) { tempAdj = "Very Cold"; } 2. if (temp >= 80) { tempAdj = "Hot"; } else if (temp >= 60) { tempAdj = "Warm"; } else if (temp >= 40) { tempAdj = "Cold"; } else { tempAdj = "Very Cold"; } 3. if (temp >= 80) { tempAdj = "Hotv; } if (temp >= 60 && temp < 80) { tempAdj = "Warm"; } if (temp >= 40 && temp < 60) { tempAdj = "Cold"; } if (temp < 40) { tempAdj = "Very Cold"; } a. II only b. III only c. I and II only d. II and III only e. I, II and III

e. I, II and III

Which of the following would print the numbers: 32 54 76 98? 1. for (int t = 32; t <= 100; t += 22) { System.out.print(t + " "); } 2. int t = 10; while (t < 90) { t += 22; System.out.print(t + " "); } 3. int t = 32; while (t < 100) { System.out.print(t + " "); t += 22; } a. I only b. II only c. III only d. II and III only e. I, II and III

e. I, II and III

The following code is intended to input three integers and print the average. What is a potential problem with the code as written? System.out.println("Please enter three integers: "); int a = scan.nextInt(); int b = scan.nextInt(); int c = scan.nextInt(); System.out.println("The average is: " + (a + b + c) / 3); a. It should be divided by 2, not 3. b. No correction needed, the code will work as written. c. It should use scan.nextDouble instead of scan.nextInt. d. The parentheses are not needed and will cause a mathematical error. e. It needs a cast so that the decimal portion will be shown.

e. It needs a cast so that the decimal portion will be shown.

What is printed by the following code? String a = "yarn" ; String b = a ; a = b.toUpperCase(); System.out.println(a + b); a. yarnyarn b. YARNYARN c. Nothing is printed. An error is caused because toUpperCase is a void method. d. yarnYARN e. YARNyarn

e. YARNyarn

Consider the following code: if (!(val >= 80 && val < 90)) Which of the following code fragments would have the same effect? a. if (val >= 80 || val < 90) b. if (!(val > 80 && val < 90)) c. if (val < 80 && val >= 90) d. if (val <= 80 && val > 90) e. if (val < 80 || val >= 90)

e. if (val < 80 || val >= 90)

What does short circuit evaluation mean in the following code? if (a < b || c != d) { System.out.print("True"); } a. if a < b is false it doesn't evaluate c != d b. if c != d is false it doesn't evaluate a < b c. if c != d is true it evaluates a < b d. if a < b is false it evaluates c != d e. if a < b is true it doesn't evaluate c != d

e. if a < b is true it doesn't evaluate c != d

Assume that x and y are boolean variables and have been properly initialized. (x || y) && !(x && y) The result of evaluating the expression above is best described as: a. always true b. always false c. true only when x is true and y is true d. true only when x and y have the same value e. only true when one of x and y is true and the other is false.

e. only true when one of x and y is true and the other is false.


Related study sets

PrepU Abram's CH 10 - Dyslipidemia

View Set

BENEFITS OF CARDIOVASCULAR TRAINING

View Set