Exam 4: Loops
Exam 1: Introduction96 Questions
Exam 2: Fundamental Data Types103 Questions
Exam 3: Decisionseasy99 Questions
Exam 4: Loops100 Questions
Exam 5: Methods94 Questions
Exam 6: Arrays and Arraylists100 Questions
Exam 7: Inputoutput and Exception Handling100 Questions
Exam 8: Objects and Classes101 Questions
Exam 9: Inheritance and Interfaces99 Questions
Exam 10: Graphical User Interfaces54 Questions
Exam 11: Advanced User Interfaces91 Questions
Exam 12: Object-Oriented Design100 Questions
Exam 13: Recursion100 Questions
Exam 14: Sorting and Searching99 Questions
Exam 15: The Java Collections Framework100 Questions
Exam 16: Basic Data Structures94 Questions
Exam 17: Tree Structures100 Questions
Exam 18: Generic Classes78 Questions
Exam 19: Streams and Binary Inputoutput82 Questions
Exam 20: Multithreading82 Questions
Exam 21: Internet Networking74 Questions
Exam 22: Relational Databases75 Questions
Exam 23: XML74 Questions
Exam 24: Web Applications74 Questions
Select questions type
What is the output of the following code snippet?
Int counter = 1;
For (double i = 0.01; i <= 1.0; i = i + 0.01)
{
Counter++;
}
System.out.println(counter);
Free
(Multiple Choice)
4.9/5
(46)
Correct Answer:
A
Insert a statement that will correctly terminate this loop when the end of input is reached.
Boolean done = false;
While (!done)
{
String input = in.next();
If (input.equalsIgnoreCase("Q"))
{
__________
}
Else
{
Double x = Double.parseDouble(input);
Data.add(x);
}
}
Free
(Multiple Choice)
4.8/5
(35)
Correct Answer:
D
When will the loop in the following code snippet stop?
Java.util.Scanner in = new java.util.Scanner(System.in);
Double sum = 0;
Int count = 0;
System.out.print("Enter values, Q to quit: ");
Do
{
Double value = in.nextDouble();
Sum = sum + value;
Count++;
System.out.print("Enter values, Q to quit: ");
}
While (in.hasNextDouble() && count < 100);
I. When the user enters an integer
II. When the user enters an alphabetic character
III. After the user enters 100 numbers
Free
(Multiple Choice)
4.8/5
(45)
Correct Answer:
D
Which code snippet produces the sum of the first n even numbers? (0 is not considered an even number.)
(Multiple Choice)
4.9/5
(31)
Choose the loop that is equivalent to this loop.
Int n = 1;
Double x = 0;
Double s;
Do
{
S = 1.0 / (n * n);
X = x + s;
N++;
}
While (s > 0.01);
(Multiple Choice)
4.8/5
(42)
What is the output of the code snippet given below?
String s = "aeiou";
Int i = 0;
Do
{
System.out.print);
I++;
If (i >= 3)
{
I = 5;
}
}
While (i < 5);
(Multiple Choice)
4.8/5
(40)
What is the last output line of the code snippet given below?
Int i = 0;
While (i < 10)
{
Int num = 1;
For (int j = i; j > 1; j--)
{
System.out.print(j + " ");
Num = num * 2;
}
System.out.println("***");
I++;
}
(Multiple Choice)
4.8/5
(46)
In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop?
Int i;
Int j;
For (i = 0; i <= 9; i++)
{
For (j = 1; j < 5; j++)
{
System.out.println("Hello");
}
}
(Multiple Choice)
4.8/5
(36)
What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5?
Double total = 0;
Boolean hasValidNumber = true;
Scanner in = new Scanner(System.in);
While (in.hasNextDouble() && hasValidNumber)
{
Double input = in.nextDouble();
If (input < 0)
{
HasValidNumber = false;
}
Else
{
Total = total + input;
}
}
System.out.println(total);
(Multiple Choice)
4.9/5
(33)
What is the output of the following code fragment?
Int i = 1;
Int sum = 0;
While (i <= 15)
{
Sum = sum + i;
I++;
}
System.out.println("The value of sum is " + sum);
(Multiple Choice)
4.9/5
(33)
How many times is the text "Let's have fun with Java." printed when this code snippet is run?
Int i = 0;
Do
{
System.out.println("Let's have fun with Java.");
I++;
If (i % 2 == 0)
{
I = 10;
}
}
While (i <= 10);
(Multiple Choice)
4.7/5
(38)
In the __________ loop header, you can include multiple update expressions, separated by commas, but it is not recommended.
(Multiple Choice)
4.8/5
(42)
How many times does the loop execute in the following code fragment?
Int i;
For (i = 0; i < 50; i = i + 4)
{
System.out.println(i);
}
(Multiple Choice)
4.9/5
(41)
Which of the following statements expresses why the following code is considered bad form?
For (rate = 5; years-- > 0; System.out.println(balance))
) . .
I. Unrelated expressions in loop header
II. Doesn't match expected for loop idiom
III. Loop iteration is not clear
(Multiple Choice)
4.8/5
(39)
What is the result when the following code is run?
Double x = 1;
Double y = 1;
Int i = 0;
Do
{
Y = x / 2;
X = x + y;
I = i + 1;
}
While (x < 2.5);
System.out.print(i + " ");
(Multiple Choice)
4.9/5
(47)
What values does counter variable i assume when this loop executes?
For (int i = 20; i >= 2; i = i - 6)
{
System.out.print(i + ",");
}
(Multiple Choice)
4.8/5
(25)
How many times does the following code snippet display "Loop Execution"?
For (int i = 0; i < 10; i++);
{
System.out.println("Loop Execution");
}
(Multiple Choice)
4.8/5
(31)
Showing 1 - 20 of 100
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)