Introduction to Java Programming: Ch. 4 quiz – Flashcards
Unlock all answers in this set
Unlock answersquestion
int x=9;
int y=8;
int z=7;
if(x > 9)
if(y > 8)
System.out.println(" x > 9 and y > 8");
else if ( z >= 7)
System.out.println(" x <= 9 and z >= 7");
else
System.out.println(" x <=9 and z < 7);
a) x <=9 and z>+7;
b) x > 9 and y > 8;
c) x <=9 and z < 7;
d) none
answer
d
question
T or F: You can always convert a switch statement to an equivalent if statement.
answer
t
question
The __________ operator can be used to compare two values.
a) numerical
b) Boolean
c) rational
d) casting
answer
c
question
Analyze the following program fragment.
int x;
double d = 15;
switch (d) {
case 1.0: x =1;
case 1.5: x =2;
case 2.0: x =3;
}
a) no errors
b) the switch control variable cannot be double
c) the program has a compiler error because the requirement break statement is missing in the switch statement.
d) the program has a compiler error because the required default case is missing in the switch statement.
answer
b
question
Suppose x=0 and y=0, what is the x after evaluating the expression (y > 0) && (1>x++)?
a) -1
b) 1
c) 0
answer
c
question
Suppose x=10 and y=10, what is x after evaluating the expression (y > 10) && (x-- > 10)?
a) 11
b) 10
c) 9
answer
b
question
Analyze the following code:
int x = 0;
if ( x > 0);
{
System.out.println("x");
}
a) the symbol x is always printed
b) nothing is printed because x > 0 is false
c) The symbol x is always printed twice
d) the value of variable x is always printed
answer
a
question
T or F: The default case must be specified in a switch statement.
answer
f
question
T or F: In a switch statement, the default case must appear last among all cases. Otherwise, it would result in a compilation error.
answer
f
question
Suppose you write the code to display "Cannot get a driver's license" if age is less than 16 and "Can get a driver's license" if age is greater than or equal to 16. Which of the following code is correct?
I:
if (age < 16)
System.out.println("Cannot get a driver's license");
if (age >= 16)
System.out.println("Can get a driver's license");
II:
if (age < 16)
System.out.println("Cannot get a driver's license");
else
System.out.println("Can get a driver's license");
III:
if (age < 16)
System.out.println("Cannot get a driver's license");
else if (age >= 16)
System.out.println("Can get a driver's license");
IV:
if (age < 16)
System.out.println("Cannot get a driver's license");
else if (age > 16)
System.out.println("Can get a driver's license");
else if (age == 16)
System.out.println("Can get a driver's license");
A. I
B. III
C. II
D. IV
answer
a b c d
question
The not equal comparison operator is Java is _________.
a) <>
b) !=
c) != =
d) ^=
answer
b
question
The following code displays ___________.
double temp = 50;
if ( temp >= 100)
System.out.println(" too hot");
else if (temp <= 40 )
System.out.println(" too cold");
else
System.out.println( " just right");
a) too cold
b) too hot too cold just right
c) just right
d) too hot
answer
c
question
Suppose isPrime is a boolean variable, which of the following is the correct and best statement for testing if isPrime is true.
A. if (isPrime = true) B. if (!isPrime = false)
C. if (!isPrime == false)
D. if (isPrime)
E. if (isPrime == true)
answer
d
question
__________ are the boolean operators.
A. ^
B. >
C. &&
D. !
answer
a b d
question
T or F: Including a space between the relational operators >=, <=, ==, and != causes a syntax error.
answer
t
question
What is y after the following switch statement is executed?
int x = 3; int y = 4;
switch (x + 3) {
case 6: y = 0;
case 7: y = 1;
default: y += 1;
}
A. 4
B. 2
C. 1
D. 3
E. 0
answer
b
question
What is the value of the following expression?
true || true && false
a) false
b) true
answer
t
question
What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?
A. true
B. false
C. There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.
answer
c
question
Which of the following operators are right-associative.
A. =
B. &&
C. + (binary +)
D. %
E. *
answer
a
question
The "less than or equal to" comparison operator in Java is __________.
A. <<
B. =<
C. <=
D. <
E. !=
answer
c