Card 17 / 76: Which of the following code segments contain invalid Java statements? It can be assumed that all variables have been properly declared and initialized. (Choose all that apply.)
A)
int x = getSize();
B)
int a = 1; int b = 2; int c = a = b;
C)
int mostPoints = 100; int score = 87; if ( mostPoints = score ) { setWinner(); }
D)
boolean stillRunning = true; if ( stillRunning = true ) { run(); }
Answer:
C) int mostPoints = 100; int score = 87; if ( mostPoints = score ) { setWinner(); }
Previous Card | ← Previous Card Button |
Next Card | → Next Card Button |
Flip Card | Space-Bar |
A, B, and D are incorrect.
A is incorrect because the statement is valid—it is a simple assignment.
B is incorrect because the code segment is valid. It is legal to have more than one variable assigned on a line as seen in the last line of B. The last line of this segment sets a equal to b, and then c equal to a.
D is incorrect because the segment is valid. Because stillRunning is a boolean value, a relational operator is not needed. Note that while this answer is legal and compiles, it may not be what was intended, as relational operators are typically used in if statements.
|