Exam 4: Conditionals and Loops

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

What happens if a case in a switch statement does not end with a break statement?

Free
(Multiple Choice)
4.9/5
(30)
Correct Answer:
Verified

D

In a nested if statement an else clause is matched to the closest unmatched if.

Free
(True/False)
4.9/5
(37)
Correct Answer:
Verified

True

The following code compiled, but while running it the program appears to hang (e.g. nothing happens). This is a clear sign of an infinite loop. What part of this code fragment caused the infinite loop? while(i < 50); { System.out.println(i); i+=2; }

Free
(Essay)
4.7/5
(30)
Correct Answer:
Verified

There is a misplace semi-colon after the while statement. This causes the loop to have an empty body, meaning that the i variable is never updated. This leads to an infinite loop.

Which of the following is not a valid relational operator in Java

(Multiple Choice)
4.8/5
(34)

Every if statement requires an associated else statement, but not every else statement requires an associated if statement.

(True/False)
4.9/5
(35)

Write a do loop that verifies that the user enters an odd value. You may assume that a Scanner object called input has already been created.

(Short Answer)
4.9/5
(27)

Write a switch statement that switches on an integer variable named val. If val is 2 or 15, then output "Hello World." For all other values, output "Goodbye World."

(Essay)
4.8/5
(37)

Write a short code fragment that uses a while loop to verify that the user enters a positive integer as input. You may assume that a Scanner object called input has already been created.

(Essay)
4.8/5
(36)

The ___________________ statement causes execution of a loop to stop, and the statement following the loop to be subsequently executed.

(Multiple Choice)
4.9/5
(40)

Which of the following statements best describes the flow of control in the main method of a Java program that has no conditionals or loops?

(Multiple Choice)
4.8/5
(33)

Write a snippet of code that determines which of two integer variables, intOne and intTwo, contains a larger number, and print out the larger one. If they are equal, the output should say that.

(Essay)
4.8/5
(39)

An infinite loop is a compile-time error.

(True/False)
4.9/5
(41)

Assume numOne and numTwo are integers. How many times might the following code print out grape? Explain your answer. if(intOne > intTwo) System.out.println(intOne + “ is larger than “ + intTwo); else if(intOne < intTwo) System.out.println(intTwo + “ is larger than “ + intOne); else System.out.println(intOne + “ and “ + intTwo + “ are equal!”);

(Essay)
4.8/5
(39)

Write a code fragment that determines how many times the character 'A' appears in a String object called name.

(Short Answer)
4.8/5
(37)

Suppose we wanted to process a text file called "input.txt" using the Scanner object. Which of the following lines of code correctly creates the necessary Scanner object?

(Multiple Choice)
4.9/5
(28)

In Java, a boolean expression is limited to having exactly 2 logical operators.

(True/False)
4.7/5
(39)

A _________________ is an object that has methods that allow you to process a collection of items one at a time.

(Multiple Choice)
5.0/5
(39)

Using a while loop, write a code fragment that computes the sum of the first n natural numbers (i.e. 1, 2, 3, 4, ... n), where n is an integer variable already defined in the program.

(Short Answer)
4.8/5
(25)

Which of the following expressions best represents the condition "if the grade is between 70 and 100"?

(Multiple Choice)
4.9/5
(31)

What is output by the following code fragment? int num = 0; int max = 10; while(num < max) { System.out.println(num); num += 2; }

(Short Answer)
4.8/5
(41)
Showing 1 - 20 of 40
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)