Filters
Question type

Study Flashcards

Show the output that would occur from the following code, including proper spacing. for (j = 0; j < 5; j++) { for (k = 0; k < 5; k++) if (j!=k) System.out.print(' '); else System.out.print('*'); System.out.println( ); }

Correct Answer

verifed

verified

*
*
*
*
*
The outer loop iterates from 0...

View Answer

A data verification loop is a loop that asks the user to input a value within a restricted range repeatedly until the user has input a value within the requested range. Write a data verification loop that that inputs an int value x within the range 0 and 100. Assume cs1.Keyboard has been imported.

Correct Answer

verifed

verified

int x;
do {
System.out.println...

View Answer

The following nested loop structure will execute the inner most statement (x++) how many times? For (int j = 0; j < 100; j++) For (int k = 100; k > 0; k--) X++;


A) 100
B) 200
C) 10,000
D) 20,000
E) 1,000,000

F) A) and C)
G) None of the above

Correct Answer

verifed

verified

Rewrite the following nested if-else statement as an equivalent switch statement. if (letter == 'A' | | letter == 'a') System.out.println("Excellent"); else if (letter == 'B' | | letter == 'b') System.out.println("You can do better"); else if (letter == 'C' | | letter == 'c') System.out.println("Try harder"); else if (letter == 'D' | | letter == 'd') System.out.println("Try much harder"); else System.out.println("Try another major! ");

Correct Answer

verifed

verified

switch (letter) { case 'A', 'a' : System.out.println("Excellent"); break; case 'B', 'b' : System.out.println("You can do better"); break; case 'C', 'c' : System.out.println("Try harder"); break; case 'D', 'd' : System.out.println("Try much harder"); break; default : System.out.println("Try another major! "); }

In Java, it is possible to create an infinite loop out of while and do loops, but not for-loops.

A) True
B) False

Correct Answer

verifed

verified

The following code has a syntax error immediately before the word else. What is the error and why does it arise? Fix the code so that this statement is a legal if-else statement. if (x < 0) ; x++; else x--;

Correct Answer

verifed

verified

The error is "else without if" and it ar...

View Answer

Each case in a switch statement must terminate with a break statement.

A) True
B) False

Correct Answer

verifed

verified

False

It is possible to convert any type of loop (while, do, or for) into any other.

A) True
B) False

Correct Answer

verifed

verified

Write a set of code that outputs all of the int values between 1 and 100 with 5 values per line, and each of those 5 values spaced out equally. Use a single for-loop to solve this problem.

Correct Answer

verifed

verified

for (int j = 1; j <= 100; j++)...

View Answer

Consider the following paint method to answer the questions below: public void paint(Graphics page) { int x, y = 200; page.setColor(Color.blue) ; for (x = 100; x < 200; x += 20) page.fillRect(x, y, 10, y-x) ; } -The size of each rectangle (bar)


A) increases in height while staying the same width
B) increases in width while staying the same height
C) increases in both width and height
D) stays the same size
E) decreases in height while staying the same width

F) B) and D)
G) A) and B)

Correct Answer

verifed

verified

E

You might choose to use a switch statement instead of nested if-else statements if


A) the variable being tested might equal one of several hundred int values
B) the variable being tested might equal one of only a few int values
C) there are two or more int variables being tested, each of which could be one of several hundred values
D) there are two or more int variables being tested, each of which could be one of only a few values
E) none of the above, you would never choose to use a switch statement in place of nested if-else statements under any circumstance

F) B) and C)
G) A) and B)

Correct Answer

verifed

verified

Given the following switch statement where x is an int, answer the questions below switch (x) { case 3 : x += 1; case 4 : x += 2; case 5 : x += 3; case 6 : x++; case 7 : x += 2; case 8 : x--; case 9 : x++ } -The statement if (x < 0) y = x; else y = 0; can be rewritten using a conditional operator as


A) y = (x < 0) ? x : 0;
B) x = (x < 0) ? y : 0;
C) (x < 0) ? y = x : y = 0;
D) y = (x < 0) ;
E) y = if (x < 0) x : 0;

F) B) and C)
G) None of the above

Correct Answer

verifed

verified

Given the following switch statement where x is an int, answer the questions below switch (x) { case 3 : x += 1; case 4 : x += 2; case 5 : x += 3; case 6 : x++; case 7 : x += 2; case 8 : x--; case 9 : x++ } -If x is currently equal to 3, what will the value of x be after the switch statement executes?


A) 5
B) 6
C) 11
D) 10
E) 12

F) B) and E)
G) A) and B)

Correct Answer

verifed

verified

A continue statement


A) may be used within a while or a do-while loop, but not a for loop
B) is identical to a break statement within Java loops
C) may be used within any Java loop statement
D) may be used within a for loop, but not within a while or a do-while loop
E) none of the above

F) A) and C)
G) A) and B)

Correct Answer

verifed

verified

The following for-loop is odd in that the loop increment value changes during iterations of the loop. Determine the number of times the loop iterates and the final value of j after the loop terminates. int k = 0; for (j = 0; j < 20; j += k) k++;

Correct Answer

verifed

verified

6 times with j = 21.
Explanation: The fi...

View Answer

Given that s is a String, what does the following loop do? For (int j = s.length( ) ; j > 0; j--) System.out.print(s.charAt(j-1) ) ;


A) it prints s out backwards
B) it prints s out forwards
C) it prints s out backwards after skipping the last character
D) it prints s out backwards but does not print the 0แต—สฐ character
E) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0

F) A) and E)
G) B) and E)

Correct Answer

verifed

verified

Write some code that inputs a set of int values and computes its average. Ask the user first how many int values will be input. Make sure that your code cannot produce a division by zero error. Assume that cs1.Keyboard has been imported.

Correct Answer

verifed

verified

int numberOfValues, j, current, sum = 0;...

View Answer

How many times will the following loop iterate? Int x = 10; Do { System.out.println(x) ; X--; } while (x > 0) ;


A) 0 times
B) 1 time
C) 9 times
D) 10 times
E) 11 times

F) C) and D)
G) A) and E)

Correct Answer

verifed

verified

Consider the following paint method to answer the questions below: public void paint(Graphics page) { int x, y = 200; page.setColor(Color.blue) ; for (x = 100; x < 200; x += 20) page.fillRect(x, y, 10, y-x) ; } -This paint method will draw several bars (sort of like a bar graph) . How many bars will be displayed?


A) 4
B) 5
C) 6
D) 10
E) 20

F) None of the above
G) A) and B)

Correct Answer

verifed

verified

Write a paint method to draw out an American flag. The flag will comprise a blue square in the upper left corner and 13 red and white stripes, starting with red, and alternating. 6 of the 13 stripes appear to the right of the blue square and the last 7 stripes appear beneath the blue square extending as far right as the other stripes. Use a for-loop to draw the stripes. Do not try to draw any stars in the blue square.

Correct Answer

verifed

verified

public void paint(Graphics page)
{
page....

View Answer

Showing 1 - 20 of 36

Related Exams

Show Answer