All questions are independent of each other.
For the multiple choice questions (MC), enter the capital letter (sorry I got a lil bit lazy here) and click the submit button at the bottom when done to get your score
The free-response questions (FR) won't be graded, but answers will be provided
1) (MC) What will be printed out?
int bobsAge = 19; int alicesAge = 25; bobsAge = alicesAge; alicesAge = 20; System.out.println("Bob is " + bobsAge + " and Alice is " + alicesAge);
A. Bob is 19 and Alice is 25
B. Bob is 20 and Alice is 20
C. Bob is 25 and Alice is 20
D. Bob is 25 and Alice is 25
E. Bob is 19 and Alice is 20
2) (MC) What will be printed out?
int first = 11; if (first < 0 || first > 10) first = 5; first = 20; System.out.println("First is " + first);
A. First is 11
B. First is 5
C. First is 20
3) (MC) Which is equivalent to the given code snippet?
if (first < 10 || first > 10) { first++; }
A.
if (first != 10) { first++; }
B.
if (first == 10) { first++; }
C.
if (first >= 10 && first <= 10) { first++; }
D. None of these
4) (MC) What will be printed out?
for (int foo = 0; foo < 2; foo++) { for (int bar = 0; bar < 3; bar++) { System.out.println(foo + " " + bar); } }
A.
0 0
0 1
1 0
1 1
2 0
2 1
B.
0 0
1 0
2 0
0 1
1 1
2 1
C.
0 0
0 1
0 2
1 0
1 1
1 2
D.
0 0
1 1
2 2
5) (FR) What are the types of each variable?
int value = 0; String text = "Hey"; boolean test = false;
value
is an int
text
is a String
test
is a boolean
6) (MC) What will be printed out?
int a = 1; int b = 2; int c = 3; System.out.println(a+b+c+":"+a+(b+c));
A. 123:123
B. 123:15
C. 6:123
D. 6:15
E. 6:6
7) (FR) What is wrong with the following code?
int t = 3; if (t == 3) { System.out.println("t is " + t); System.out.println("Hi"); System.out.println("Hey");
missing closing curly brace for if
statement
8) (FR) What will be printed out?
int a = 6; switch(a) { case 5: System.out.println("five"); case 6: System.out.println("six"); case 7: System.out.println("seven"); }
six
seven
9) (MC) Assume the user enters "Hello" when prompted, what will be printed out?
Scanner scanner = new Scanner(System.in); String text = scanner.next(); if (text == "Hello") { System.out.println(text); } else { System.out.println("Goodbye"); }
A. There's no error, but nothing will be printed out
B. There's an error!
C. Hello
D. Goodbye
E. text
10) (FR) What is wrong with the following code?
int n = "Testing"; System.out.println(n);
n
is an int
but it is being given a String
value
11) (MC) Assume a
, b
, c
, and d
are int
s. Which expression does NOT give the same answer as the others?
A. a*b+c*d
B. (a*b+c*d)
C. a*(b+c)*d
D. (a*b)+(c*d)
12) (MC) What is the result of running the code?
int a = 6; int b = 3; a = b; b = a;
A. a
is equal to 6 and b
is equal to 6
B. a
is equal to 6 and b
is equal to 3
C. a
is equal to 3 and b
is equal to 6
D. none of the above
13) (FR) What is wrong with the following code?
int x = 10; if (x == 10); x--; }
there's a semicolon after the if
condition
missing an open curly brace after the if
condition
14) (MC) What will be printed out?
int a = 5, b = 4, c = 3; System.out.println(a + (b + c) + " = " + (a + b) + c);
A) 12=543
B) 12=93
C) 93=57
D) 93=543
E) None of these
15) (MC) What is the result of running the code?
int a = 6; int b = 3 + a; b = a + 3; b = b + a;
A) a
is equal to 6 and b
is equal to 9
B) a
is equal to 6 and b
is equal to 18
C) a
is equal to 9 and b
is equal to 9
D) a
is equal to 15 and b
is equal to 15
E) a
is equal to 6 and b
is equal to 15
16) (MC) What will be printed out?
int b = 9; b = b + (b = 3); System.out.println(b);
A) 3
B) 6
C) 9
D) 12
E) There's an error!
17) (MC) What will be printed out?
int first = 11 int second = 13; int third = 15; System.out.println(first+1 + second+2 + third+3);
A) 45
B) 44
C) 40
D) 39
E) There's an error!
18) (MC) Assuming the user enters "yes" when prompted, what will be printed out?
Scanner s = new Scanner(System.in); String choice = s.next(); if (choice.equals("Yes")) { System.out.println("Yay!"); } else if (choice.equals("No")) { System.out.println("Nay!"); } else { System.out.println("Huh?"); }
A) Nay!
B) Yay!
C) Huh?
D) There's an error!
E) There's no error, but nothing is printed out
19 (FR) What is wrong with the following code?
int a = 78; int b = 907; int a = b + a; System.out.println(a); System.out.println(b);
on the third line, we are declaring a variable called a
, but a
was already declared on the first line
the third line should be: a = b + a;
20) (MC) What will be printed out?
String a = "HEY THERE"; if (a.equalsIgnoreCase("heythere")) { System.out.println("Success!"); } else if (a.equalsIgnoreCase("hey there")) { System.out.println("Also Success!"); }
A) There's an error!
B) Success!
C) There's no error, but nothing is printed out
D) Also Success!
21) (MC) How many times is y
printed out?
int y = 10; while (y > 0) { if (y == 5) { continue; } System.out.println(y); y--; }
A) 10
B) 5
C) There's no error, but nothing is printed out
D) 9
E) There's an infinite loop!
22) (MC) How many times is i
printed out?
for (int i = 0; i < 20; i++) { if (i % 2 == 1) { continue; } System.out.println(i); }
A) 10
B) 20
C) 18
D) 1
E) There's an error!
23) (FR) The program will ask the user to enter a number. If the number is between 7 and 10, the user will be asked if they want a prize. Then, if the user enters "yes", the program will print out, "Here's a cookie". If the user enters a number not between 7 and 10, the program will print out, "Bad guess. Better luck next time!". Will this code do what is being described above?
Scanner s = new Scanner(System.in); int guess = s.nextInt(); String choice = "yes"; if (guess > 7 && guess < 10) { System.out.println("Good guess! Want a prize?"); choice = s.next(); } if (choice.equalsIgnoreCase("yes")) { System.out.println("Here's a cookie"); } else { System.out.println("Bad guess. Better luck next time!"); }
yes and no
yes
no
one correct solution is:
Scanner s = new Scanner(System.in); int guess = s.nextInt(); String choice = "yes"; if (guess > 7 && guess < 10) { System.out.println("Good guess! Want a prize?"); choice = s.next(); if (choice.equalsIgnoreCase("yes")) { System.out.println("Here's a cookie"); } } else { System.out.println("Bad guess. Better luck next time!"); }
notice the position of the if
statements
24) (MC) How many lines will be printed out?
for (int i = 0; i < 6; i++) { for (int j = 0; j < 4; j++) { System.out.println("Hi"); } }
A) There's an error!
B) 24
C) 10
D) 12
E) 35
25) (MC) What will be the last number printed out?
int k = 0; for (int i = 0; i < 10; i+=2) { for (int j = 0; j < 5; j++) { k = k + 1; System.out.println(k); } }
A) 15
B) 25
C) There's an error!
D) 50
E) 10
26) (FR) What are the different ways to change the code so that it results in an infinite loop?
int index = 0; while (index < 10) { System.out.println("Boo"); index++; }
remove the index++
line
change the while
condition to while (index >= 0)
(or ≥ than some negative number)
add index--
(or decrease index
by some amount)
add index = 0
(or set index
equal to some amount < 10)
and other possibilities
27) (FR) Write a program that prints out all even numbers from 1 to 25 using a for
loop and then prints out all odd numbers from 25 to 1 using a while
loop. The even numbers should be space separated and all on one line. The odd numbers should be on a different line from the even numbers, but also space separated and all on one line. For example, the output should look like:
2 4 6 8 10 12 14 16 18 20 22 24
25 23 21 19 17 15 13 11 9 7 5 3 1
for (int i = 1; i < 25; i++) { if (i % 2 == 0) { System.out.print(i + " "); } } System.out.println(); int i = 25; while (i > 0) { if (i % 2 == 1) { System.out.print(i + " "); } i--; }
28) (FR) Is there anything wrong with this code?
Scanner s = new Scanner(System.in); String text = s.next(); if (text == "yes") { System.out.println("yes"); } else { System.out.println("no"); }
should not be using ==
to check for equality of String
s
29) (FR) Is there anything wrong with this code?
Scanner s = new Scanner(System.in); int input = s.nextInt(); if (input = 5) { System.out.println("you entered 5"); } else { System.out.println("you did not enter 5"); }
should not be using =
to check for equality
30) Yay!
See MC Answers