Computer Science: Application Multiple Choice HIGHLIGHT THE CORRECT ANSWER

1. Which code segment will print “Hello Karel” to the screen in Java?

System.out.printLine(“Hello Karel”);

print “Hello Karel”;
System.out.println(“Hello Karel”);
System.println(“Hello Karel”);

2. What will this code segment output?

System.out.println(“Hello”);

System.out.println(“World”);

Hello

World

HelloWorld
Hello World
Hello

World

3. Which of the below is not a primitive type in Java?

int
char
boolean
double
apple

4. Which of the following is a proper way to declare and initialize a variable in Java?

myInteger = 100;
char = ‘a’;
int myNumber = 10;
“Variable”

5. Which of these input methods was not introduced in the video?

readInt
readDouble
readChar
readBoolean

6. What will this code segment output if the user inputs the number 10?

int yourInteger = readInt(“Please input a number”);

System.out.println(yourInteger);

yourInteger
“yourInteger”
10
“10”

7. What is the result of this expression?

150 % 100

0
100
50
3

8. Which of the below is NOT a Java arithmetic operator?

+
#
/

9. What is casting in Java?

What Karel does when she breaks her leg.
Casting is turning a value of one type into another type.
A way to get user input in Java.
A way to print to the screen in Java.

10. What will this java expression evaluate to?

(int) 9.9

0
10
9
5

11. What is a boolean in Java?

A true or false value
A very precise number
A single character
A sequence of characters

12. Which of the below is an example of a boolean?

10.1
“true”
‘F’
true

13. Which of the below is a logical operator?

!
##
**
%

14. What does this Java expression evaluate to?

true && !false

true
false

15. Which of the following is NOT a comparison operator?

<
?
==
>=

16. What does this Java expression evaluate to?

80 >= 80

“true”
true
false
This expression will error

17. Why do we use for loops in Java?

To break out of some block of code
To do something if a condition is true
To do something while a condition is true
To repeat something for a fixed number of times

18. Which for loop will properly print “hello” 10 times?

A

for(int i = 0; i < 10; i++)

{

System.out.println(“hello”);

}

B

for(int i = 10)

{

System.out.println(“hello”);

}

C

for(int i = 0; i++; i < 10)

{

System.out.println(“hello”);

}

D

for(int i = 10; i < 0; i++)

{

System.out.println(“hello”);

}

A
B
C
D

19. Why do we use while loops in Java?

To break out of some block of code
To do something if a condition is true
To repeat some code while a condition is true
To repeat something for a fixed number of times

20. What will this while loop do?

while(true)

{

System.out.println(“Hello”);

}

Print Hello one time
Print Hello in an infinite loop
Print Hello until program receives user input
Nothing

21. Why do we use if statements in Java?

To break out of some block of code
To do something only if a condition is true
To do something while a condition is true
To repeat something for a fixed number of times

22. Which if statement is written properly?

if (boolean expression):

// code to execute

if boolean expression

{

// code to execute

}

if(boolean expression)

{

// code to execute

}

if boolean expression

[

// code to execute

]

23. Which statement can we use inside of a loop to end it?

System.out.println;
break;
SENTINEL;
else

24. Which of the following is correct loop and a half structure?

while(condition)

{

// code

}

while(true)

{

// code

if(condition)

{

break;

}

// code

}

for(int i = 0; i <= 1.5; i++)

{

// code

}

for(int i = 0; i < 2; i++)

{

// code

break;

// code

}

25. In the following line of code:

boolean shortCircuit = true || (5 / 0 == 0);

Will the 5 / 0 == 0 operation be evaluated?

Yes
No

26. In the following line of code:

boolean shortCircuit = true && (5 / 0 == 0);

Will the 5 / 0 == 0 operation be evaluated?

Yes
No

27. Which of the following logical statements is equivalent to

!(A && B)

Where A and B are boolean values

!A && B
!A && !B
!A || !B
!A || B

28. Which of the following logical statements is equivalent to:

!(A || B)

Where A and B are boolean values

!A && !B
!A || !B
!A || B
A && B

29. What is a String type in Java?

An extremely large integer value
A sequence of integers
A single character
A sequence of characters

30. What is the proper way to compare String values in Java?

The == operator
The .equals() String method
The = operator
The | operator

31. What is the name of the method to print out text in Java?

System.out.println()
System.out.printline()
System.output()
System.out.PRINTLN()

32. What is the output of the following lines?

int x = 24;

System.out.println(“The total is ” + x + x);

The total is 24
The total is 2424
The total is 48
The total is x + x

33. Which of the following is not a primitive type?

int
double
String
boolean
char

34. What is the value of x after this code?

int x = 5;

x = 10;

x = 4;

5
10
4
true

35. What is the proper syntax to initialize a double called temperature to have the value 70.4?

int temperature = 70.4;
double temperature = 70.4;
temperature = 70.4;
float temperature = 70.4;

36. What is the result of this expression?

5 + 2 / 3 + 1

5
6
6.67
0

37. Which expression returns the 1’s place of an integer x?

x % 10
x / 10
x % 100
x + 1

38. What is the value of myInteger after this line of code?

int myInteger = (int) 5.6;

6
5.5
5
9

39. Which expression is true?

true && !true
!false || !true
true && false
false || false || !true

40. Which of these is not a logical operator?

&&
!
||
++

41. What is the output of this for loop?

for(int i = 0; i < 100; i += 2)

{

System.out.println(i);

}

The even numbers from 0 to 98, inclusive
The even numbers from 0 to 100, inclusive
All of the numbers from 0 to 100
The odd numbers from 0 to 98, inclusive

42. What is the output of this for loop?

for(int i = 10; i > 2; i -= 3)

{

System.out.println(i);

}

10  7  4
10  7  4  1
1  2  3
10  8  6  4  2

43. How many times will the loop execute?

int i = 0;

while(i < 50)

{

System.out.println(i);

i += 10;

}

0
4
5
50

44. What will this code output?

if(true && true && false)

{

System.out.println(“Hello Karel”);

}

if(true && 4 == 2 + 2)

{

System.out.println(“Second if statement!”);

}

Hello Karel
Hello Karel

Second if statement!

This program will print nothing
Second if statement!

45. What will this program print if the value of grade is 80?

if(grade > 90)

{

System.out.println(“A”);

}

else if(grade > 80)

{

System.out.println(“B”);

}

else if(grade > 70)

{

System.out.println(“C”);

}

A
B
C
Nothing

46. What output will be produced by

System.out.println(“Hello”);

System.out.println(“Karel”);

Hello Karel
HelloKarel
Hello

Karel

Error

47. What will the values of x and y be after this code segment runs?

int x = 100;

int y = 100;

if (x <= 100)

{

if (y > 100)

{

x = 200;

}

else

{

x = 99;

}

}

else

{

x++;

}

y = x + y;

x = 100

y = 200

x = 101

y = 100

x = 101

y = 201

x = 99

y = 199

48. What will the code segment output?

for (int m = 5; m > 0; m–)

{

for(int n = m; n > 0; n–)

{

System.out.print(“*”);

}

System.out.println();

}

*

**

***

****

*****

*****

****

***

**

*

*****

*****

*****

*****

*****

***************

49. Refer to the following code segment:

double myDouble = 1/4;

System.out.println(“1 / 4 = ” + myDouble);

The output of the code is:

1 / 4 = 0.0

The student wanted the output to be:

1 / 4 = 0.25

Which change to the first line of their code segment would get the student the answer that they wanted?

int myDouble = 1/4;
double myDouble = (double) 1/4;
double myDouble = (int) 1/4;
double myDouble = (int) (1.0/4.0);

50. What is the result of this expression?

4 + 8 * 3 / 4 + 5 % 2

5
6
12
11

51. Which of these is not a valid Java method name?

runInCircles
jumpHurdle
find tower
finishMaze

52. Which of these is not a valid Java method name?

spin10Times
10TimesMove
moveTenTimes
spinTenTmes

53. Which of these characters can a Java method name start with?

(I) Letters

(II) Numbers

(III) Underscore

(IV) $ Sign

I only
I and II
II only
I and III
I, III, and IV

54. What in this code segment could potentially cause a bug in our program?

String myString = readLine(“What is your name?”);

if (myString == “Karel”)

{

System.out.println(“Hi Karel!”);

}

else

{

System.out.println(“You’re not Karel!”);

}

Syntax error in the if statement
Nothing
Trying to store a Line in a String variable.
Comparing Strings with == instead of the .equals method.

55. What is the output of this program?

int sum = 1;

System.out.println(“Welcome to the adding machine!”);

while(sum < 10)

{

sum += sum;

System.out.println(sum);

}

Welcome to the adding machine!  1  2  4  8  16
Welcome to the adding machine!  2  4  8  16
Welcome to the adding machine!  2  4  8
This code has an infinite loop.