AP Java Unit 2 Quiz

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

What is the name of the method to print out text in Java? A: System.out.println() B: System.out.printline() C: System.output() D: System.out.PRINTLN()

A

What is the output of this program? EX: int sum = 1; System.out.println("Welcome to the adding machine!"); while(sum < 10) { sum += sum; System.out.println(sum); } A: Welcome to the adding machine! 1 2 4 8 16 B: Welcome to the adding machine! 2 4 8 16 C: Welcome to the adding machine! 2 4 8 D: This code has an infinite loop.

B This loop actually is not infinite. It will end once sum is greater than or equal to 10. At each iteration of the loop, sum will be 1, 2, 4, 8, 16 and then stop at 16 because 16 > 10.

Which of the following is not a primitive type? A: int B: double C: String D: boolean E: char

C

What is the result of this expression? EX: 4 + 8 * 3 / 4 + 5 % 2 A: 5 B: 6 C: 12 D: 11

D

What is the output of the following lines? EX: int x = 24; System.out.println("The total is " + x + x); A: The total is 24 B: The total is 2424 C: The total is 48 D: The total is x + x

B In this case, the plus sign does not add. Remember, when working with Strings the plus sign concatenates rather than adds.

Which of these is not a valid Java method name? A: spin10Times B: 10TimesMove C: moveTenTimes D: spinTenTmes

B Method names cannot begin with a number.

What is the result of this expression? A: 5 B: 6 C: 6.67 D: 0

B Remember the proper order of operations: division occurs before addition.

What is the value of myInteger after this line of code? EX: int myInteger = (int) 5.6; A: 6 B: 5.5 C: 5 D: 9

C

What is the value of x after this code? EX: int x = 5; x = 10; x = 4; A: 5 B: 10 C: 4 D: true

C

What output will be produced by EX: System.out.println("Hello"); System.out.println("Karel"); A: Hello Karel B: HelloKarel C: Hello Karel D: Error

C

What will this program print if the value of grade is 80? EX: if(grade > 90) { System.out.println("A"); } else if(grade > 80) { System.out.println("B"); } else if(grade > 70) { System.out.println("C"); } A: A B: B C: C D: Nothing

C

Which of these is not a valid Java method name? A: runInCircles B: jumpHurdle C: find tower D: finishMaze

C

What in this code segment could potentially cause a bug in our program? EX: String myString = readLine("What is your name?"); if (myString == "Karel") { System.out.println("Hi Karel!"); } else { System.out.println("You're not Karel!"); } A: Syntax error in the if statement B: Nothing C: Trying to store a Line in a String variable. D: Comparing Strings with == instead of the .equals method.

D

What will this code output? EX: if(true && true && false) { System.out.println("Hello Karel"); } if(true && 4 == 2 + 2) { System.out.println("Second if statement!"); } A: Hello Karel B: Hello Karel Second if statement! C: This program will print nothing D: Second if statement!

D

What will the values of x and y be after this code segment runs? EX: int x = 100; int y = 100; if (x <= 100) { if (y > 100) { x = 200; } else { x = 99; } } else { x++; } y = x + y; A: x = 100 y = 200 B: x = 101 y = 100 C: x = 101 y = 201 D: x = 99 y = 199

D First, x is less than or equal to 100 is true, so the branch is taken. Then, y is not greater than 100, so the else branch is taken and `x` becomes 99. Finally, `y` becomes 99 + 100, leading to 199.

Which of these is not a logical operator? A: && B: ! C: || D: ++

D This is used to increment a value (equivalent to adding 1).

Which expression is true? A: true && !true B: !false || !true C: true && false D: false || false || !true

B

What is the output of this for loop? EX: for(int i = 0; i < 100; i += 2) { System.out.println(i); } A: The even numbers from 0 to 98, inclusive B: The even numbers from 0 to 100, inclusive C: All of the numbers from 0 to 100 D: The odd numbers from 0 to 98, inclusive

A

What is the output of this for loop? EX: for(int i = 10; i > 2; i -= 3) { System.out.println(i); } A: 10 7 4 B: 10 7 4 1 C: 1 2 3 D: 10 8 6 4 2

A

Which expression returns the 1's place of an integer x? A: x % 10 B: x / 10 C: x % 100 D: x + 1

A

What will the code segment output? EX: for (int m = 5; m > 0; m--) { for(int n = m; n > 0; n--) { System.out.print("*"); } System.out.println(); } A: * ** *** **** ***** B: ***** **** *** ** * C: ***** ***** ***** ***** ***** D: ***************

A Don't forget about the newline character that is printed!

Refer to the following code segment: EX: double myDouble = 1/4; System.out.println("1 / 4 = " + myDouble); The output of the code is: EX: 1 / 4 = 0.0 The student wanted the output to be: EX: 1 / 4 = 0.25 Which change to the first line of their code segment would get the student the answer that they wanted? A: int myDouble = 1/4; B: double myDouble = (double) 1/4; C: double myDouble = (int) 1/4; D: double myDouble = (int) (1.0/4.0);

B

What is the proper syntax to initialize a double called temperature to have the value 70.4? A: int temperature = 70.4; B: double temperature = 70.4; C: temperature = 70.4; D: float temperature = 70.4;

B

How many times will the loop execute? EX: int i = 0; while(i < 50) { System.out.println(i); i += 10; } A: 0 B: 4 C: 5 D: 50

C The counter begins at 0 and increases by 10. It will execute 5 times, when i = 0, 10, 20, 30, and 40.

Which of these characters can a Java method name start with? (I) Letters (II) Numbers (III) Underscore (IV) $ Sign A: I only B: I and II C: II only D: I and III E: I, III, and IV

E Method names cannot begin with a number.


संबंधित स्टडी सेट्स

Structural Kinesiologys, Body Planes, Directional Terms

View Set

Pharm Chapter 38: Agents to Control Blood Glucose Level

View Set

The Road study guide sections 1-5

View Set