0% found this document useful (0 votes)
21 views

Cs2e Midterm

The document contains examples of code snippets and questions testing knowledge of Java programming concepts like loops, conditional statements, and order of operations. It includes multiple choice questions, true/false questions, and examples to write out or complete. The last section asks to rewrite a while loop as a do-while loop, write a program to order user input, and explain the difference between do-while and while loops.

Uploaded by

shancchi081416
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Cs2e Midterm

The document contains examples of code snippets and questions testing knowledge of Java programming concepts like loops, conditional statements, and order of operations. It includes multiple choice questions, true/false questions, and examples to write out or complete. The last section asks to rewrite a while loop as a do-while loop, write a program to order user input, and explain the difference between do-while and while loops.

Uploaded by

shancchi081416
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Instruction: Fill in the correct answer in the blank. Write your answer on your answer sheet.

1. In a__________Joop, the loop condition is evaluated after executing the body of the loop
2. Executing a________ statement in the body of a loop immediately terminates the loop
3.The________ is called the body of the loop.
4. A_________statement allows a variable to be tested for equality against a list of values.
5. A__________ is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.
1. In a do-while loop, the loop condition is evaluated after executing the body of the loop.
2. Executing a break statement in the body of a loop immediately terminates the loop.
3. The block is called the body of the loop.
4. A switch statement allows a variable to be tested for equality against a list of values.
5. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to
execute a specific number of times.

Test 2. True or False. 15 Points


Instruction: Read each statement below carefully. Place a T if you think a statement is TRUE Place an F if
you think the
statement is FALSE
1. The result of a logical expression cannot be assigned to an int variable.
2. The expression in a switch statement should evaluate to a value of any primitive data type
3. It is possible that the body of a while loop might not execute at all.
4. When a while loop terminates, the control first goes back to the statement just before the while
statement, and then the control goes to the statement immediately following the while loop.
5. The while loop:
i = 0 while (j <= 10)
j++;
terminates when j > 10

1. F (The result of a logical expression can be assigned to an int variable.)


2. F (The expression in a switch statement should evaluate to a value of integral type: byte, short,
char, or int.)
3. T (It is possible that the body of a while loop might not execute at all if the loop condition is
initially false.)
4. T (When a while loop terminates, the control first goes back to the statement just before the
while statement, and then the control goes to the statement immediately following the while
loop.)
5. F (The while loop provided does not include the increment of 'j', which would lead to an infinite
loop as 'j' will never increase, thus not terminating when 'j > 10'.)

Test 4. Programming Test. 25 Points


Instruction: Write the output of the following Java code in your answer sheet
1. How many times will the following loop run?
for (int i =2; i < 10; i = i * i){
System.out.println(i):
}
2. How many times will the following loop work?
int x = 0
int y= 5
while (x < y) {
System.out.println("Hello");
x++;
}
3. What is the output of the following code?
int a=11; int b = 12; int c = 40;
switch (a)
case 40:
System.out.println(b);
break;
default:
System.out.println(c);
}
4. What is the output of the following Java Code?
num = 5;
while (num > 5)
{
num = num + 2 ;
}
System.out.println(num);

5. State what output, if any, results in the statement:


for (i=1; i<= 5; i++)
System.out.print("*");
1=1+1;
}
System.out.println();

Test 5. Discussion/Design. 50 Points


Instruction: Answer the following questions concisely.
1. Write the while loop as a do while loop: (20 pts)
int i=0, value = 0;
while (i<=20)
{
if (i % 2 == 0 && i <= 10)
value=value +1*1;
else if (i % 2 == 0 && i>10)
value=value + i;
else
value=value-i;
i=i+1;
}
System.out.println("value="+ value);

while loop converted to a do-while loop:

int i = 0, value = 0;
do {
if (i % 2 == 0 && i <= 10)
value += 1 * 1;
else if (i % 2 == 0 && i > 10)
value += i;
else
value -= i;
i++;
} while (i <= 20);
System.out.println("value=" + value);

2. Write a program in Java where a user must enter two integers. The program should then print the two
numbers in ascending order. (20 points)
3. How is a do while loop different from a while loop? (10 points)

You might also like