1 of 45

Quiz 2 Review Game

2 of 45

How to Play

Separate players into some number of teams about the same size.

In each team, establish the order in which students will answer questions (for example, stand in a line.)

Someone should be charged with keeping score.

There are 20 questions. You may need to use fewer to be sure each team gets the same number of questions.

3 of 45

How to Play, contd

When it is your turn to answer, read the slide and say what you think the answer is. If you don’t know, you may ask your team for help.

Once you have decided on an answer, announce it to the group. If another team believes you to be wrong, they can call “False”. (The first team to call false gets it.)

4 of 45

How to Play, contd

Flip to the next slide to see the answer.

If you answered correctly by yourself, your team gets 10 points. If you answered correctly with your team, you get 5 points. If a team that called “false” they lose 5 points.

If you answered incorrectly, your team gets no points, but any team that called false gets 5 points.

5 of 45

Ready?

Here we go...

6 of 45

  1. What is the value of b?

int x = 2;

int y = 4;

boolean b = (x<3) && (y<3);

7 of 45

  • What is the value of b? FALSE

int x = 2;

int y = 4;

boolean b = (x<3) && (y<3);

8 of 45

2. What is the value of b?

int x = 2;

int y = 4;

boolean b = (x<3) || (y<3);

9 of 45

2. What is the value of b? TRUE

int x = 2;

int y = 4;

boolean b = (x<3) || (y<3);

10 of 45

3. What is the value of b?

int x = 2;

int y = 4;

boolean b = !(y<3);

11 of 45

3. What is the value of b? TRUE

int x = 2;

int y = 4;

boolean b = !(y<3);

12 of 45

4.

Which Java keyword means that a method has no output?

13 of 45

4.

Which Java keyword means that a method has no output?

VOID

14 of 45

5. What is the error?

public int mystery(double a, double b){

if (a<b){

return a;� }� return b;

}

15 of 45

5. What is the error?

public int mystery(double a, double b){

if (a<b){

return a;� }� return b;

}

The return type is “int” but it returns a double.

16 of 45

6. What is the error?

public int mystery(int a, int b){

if(a<b){

return a;

}

else if (a>=b){

return b;

}

}

17 of 45

6. What is the error?

public int mystery(int a, int b){

if(a<b){

return a;

}

else if (a>=b){

return b;

}

}

You cannot have all return statements inside “if” blocks.

At least one has to be either outside all blocks or in an “else”.

18 of 45

7. What is the value of x after the code?

int a = 3;

int b = 5;

int x;

if (a<b){

x = 0;�}

else if (b<10){

x = 1;�}

else{

x = 2;

}

19 of 45

7. What is the value of x after the code?

int a = 3;

int b = 5;

int x;

if (a<b){

x = 0;�}

else if (b<10){

x = 1;�}

else{

x = 2;

}

x = 0

20 of 45

8. What is the value of x after the code?

int a = 3;

int b = 5;

int x;

if (a<b){

x = 0;�}

if (b<10){

x = 1;�}

else{

x = 2;�}

21 of 45

8. What is the value of x after the code?

int a = 3;

int b = 5;

int x;

if (a<b){

x = 0;�}

if (b<10){

x = 1;�}

else{

x = 2;�}

x = 1

22 of 45

9. What is the value of x?

String s = “012345”;

int x = s.length();

23 of 45

9. What is the value of x?

String s = “012345”;

int x = s.length();

x = 6

24 of 45

10. What is the value of x?

String question = “Exam 2”;

int x = question.indexOf(“x”);

25 of 45

10. What is the value of x?

String question = “Exam 2”;

int x = question.indexOf(“x”);

x = 1

26 of 45

11. What is the value of x?

String s = “012345”;

int x = s.indexOf(“one”);

27 of 45

11. What is the value of x?

String s = “012345”;

int x = s.indexOf(“one”);

x = -1

28 of 45

12. What is the value of x?

String question = “Exam 2”;

String x = question.substring(1);

29 of 45

12. What is the value of x?

String question = “Exam 2”;

String x = question.substring(1);

x = “xam 2”

30 of 45

13. What is the value of x?

String question = “Exam 2”;

String x = question.substring(1,4);

31 of 45

13. What is the value of x?

String question = “Exam 2”;

String x = question.substring(1,4);

x = “xam”

32 of 45

14.

How would you find the next to last letter in a string called str?

33 of 45

14.

How would you find the next to last letter in a string called str?

either

char letter = str.charAt(str.length()-1);

Or

String letter = str.substring(str.length()-1);

34 of 45

15. Suppose string str has three stars in it (“*”)...

How would you find where the second star occurs?

35 of 45

15. Suppose string str has three stars in it (“*”)...

How would you find where the second star occurs?

Find the index of the first star.

Ask for index of star again, starting at the previously found index + 1.

int index = str.indexOf(“*”);

int index2 = str.indexOf(“*”, index + 1);

36 of 45

16. What is the value of x? (Careful!!)

String a = “str”;

String b = “str”;

boolean x = (a==b);

37 of 45

16. What is the value of x? (Careful!!)

String a = “str”;

String b = “str”;

boolean x = (a==b);

x = false. The correct way to see if strings are equal is .equals()

38 of 45

17. Tell output or error

int a = 3;�int b = 5;�int x = 5;

if (a<b){

x = 0;�}

System.out.println(x);

39 of 45

17. Tell output or error

int a = 3;�int b = 5;�int x = 5;

if (a<b){

x = 0;�}

System.out.println(x);

It prints 0. Some students mistakenly think every “if” needs an “else” but this is fine.

40 of 45

18. Tell output or error (Careful!)

int a = 13;�int b = 5;

if (a<b){

int x = 0;�}

else{

x = 10;

}

System.out.println(x);

41 of 45

18. Tell output or error (Careful!)

int a = 13;�int b = 5;

if (a<b){

int x = 0;�}

else{

x = 10;

}

System.out.println(x);

Error. The x variable is created in the if block and cannot be used outside that block.

42 of 45

19. What is x after the code?

int a = 3;�int b = 5;�int x = 10;

if (a<b && b <x){� x = 0;�}�else if (a==3){� x = 1;�}

if(x == 0 || b > a){

x = x + a;

}

43 of 45

19. What is x after the code?

int a = 3;�int b = 5;�int x = 10;

if (a<b && b <x){� x = 0;�}�else if (a==3){� x = 1;�}

if(x == 0 || b > a){

x = x + a;

}

X = 3

44 of 45

20. What is x after the code?

int a = -3;�int b = 5;�int x = 10;

if !(a<b || b > x){� x = 0;�}�if (a!=-3){� x = x + 1;�}

else{

x = x + a;

}

45 of 45

20. What is x after the code?

int a = -3;�int b = 5;�int x = 10;

if !(a<b || b > x){� x = 0;�}�if (a!=-3){� x = x + 1;�}

else{

x = x + a;

}

X = 7