logo

APSU Notes


Quiz 4

Question 1

Given the following Java code:

1
2
3
4
if (number < 10)
    System.out.println("banana");
else
    System.out.println("artichoke");

What is displayed if the variable has a value of 12?

articoke

Question 2

Given the following Java code:

1
2
3
4
if (number <= 8)
    System.out.println("rutabaga");
if (number >= 5)
    System.out.println("kumquat");

What is displayed if the variable number has a value of 6?

1
2
rutabaga
kumquat

Question 3

Which of the following boolean expressions evaluates to true if num1 is not equal to num2?

num1 != num2

Quesiton 4

Given the following Java code:

1
2
3
4
if (!(number < 16))
    System.out.println("watermelon");
else
    System.out.println("honeydew");

What is displayed if the value of number is 13?

honeydew

Question 5

Which of the following is the correct way to test if two strings s1 and s2 are exactly the same?

s1.equals(s2);

Question 6

Which of the following is the correct way to test if two strings s1 and s1 are stored in the same memory location?

s1 == s2

Question 7

Given the following Java code:

1
2
3
4
5
6
7
if (number < 16)
    if (number > 12)
        System.out.println("cabbage");
    else
        System.out.println("lettuce");
else
    System.out.println("spinach");

What is displayed to screen if the value of number is 10?

lettuce

Question 8

Given the following Java code:

1
2
3
4
5
6
7
if (number < 16)
    if (number > 12)
        System.out.println("cabbage");
    else
        System.out.println("lettuce");
else
    System.out.println("spinach");

What is displayed to the screen if the value of number is 24?

spinach

Question 9

Which of the following conditional operators should you avoid using with floating point data types?

==

Question 10

What is the value assigned to the variable number by the following statements?

1
number = (4 < 3) ? 24 : 13;

13