0% found this document useful (0 votes)
315 views4 pages

Java Programming 4th Quarter

The program prompts the user to enter a positive integer n. It then uses a for loop to calculate the sum from 1 to n. The sum is printed out at the end.

Uploaded by

Jane F Moraca
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
315 views4 pages

Java Programming 4th Quarter

The program prompts the user to enter a positive integer n. It then uses a for loop to calculate the sum from 1 to n. The sum is printed out at the end.

Uploaded by

Jane F Moraca
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

Republic of the Philippines

Department of Education
Region VII, Central Visayas
DIVISION OF CITY OF BOGO
CITY OF BOGO SCIENCE AND ARTS ACADEMY
St. Joseph Vill., Cogon, Bogo City, Cebu

FOURTH QUARTER PERIODIC EXAM


Computer Programming (Java)

Name: _____________________________________________ Track/Sec:________________ Date:_____________ Score: _______

Help your classmate build a trust on himself/herself. Do not let him/her cheat.

TEST I. MULTIPLE CHOICE. Encircle the letter of the correct answer. Erasure means WRONG.
1. What character is used to terminate a java statement?
a. ; c. \
b. } d. )

2. How do you instantiate a Scanner class?


a. Scanner myScanner = new Scanner(system.in); c. Scanner myScanner = new Scanner(System.in);
b. Scanner MyScanner = new Scanner(System.in); d. Scanner myScanner = new Scanner(System.In);

3. What represents the condition? for(int j = 11; j > -2; j -= 2)


a. j > -2 c. j = 11
b. j -= 2 d. j - -

4. What type of loop is ideal in situations where the exact number of iteration is known?
a. for loop c. do…while Loop
b. while loop d. none of the above

5. What is the correct way of importing Scanner class from a library?


a. Import java.util.Scanner; c. import java.util.scanner;
b. Import java.util.scanner; d. import java.util.Scanner;

6. A continue statement causes the execution to skip to _______. * Each pass through a loop is called iteration
a. at the end of the program c. the statement following the continue statement
b. the first statement after the loop d. the next iteration of the loop

7. A for loop inside another for loop is called a/an _____ loop
a. Bracketed c. Inverted
b. Nested d. Negative

8. If there is more than one statement in the block of a for loop, which of the following must be placed at the
beginning and the ending of the loop block?
a. parentheses ( ) c. brackets [ ]
b. French curly braces { } d. arrows < >

9. Which of the following loop does not terminate?


a. for( int i=0; i<100; i=10){…} c. for( int i=100; i>100; i-=10){…}
b. for( int i=10; i!=1000; i*=10){…} d. for( int i=0; i<10; i+=1){…}

10. What’s wrong? for(int k = 2, k <= 12, k++)


a. the increment should always be ++k
b. the variable must always be the letter i when using a for loop
c. there should be a semicolon at the end of the statement
d. the commas should be semicolons

11. A loop that does not terminate is called _________ loop.


a. Cascaded c. Recursive
b. Nested d. Infinite

12. What is the value of m when you exit the loop?


for (int m = 30; m > 0; m = m-4){
System.out.println(m);
}
a. 4 c. -4
b. 0 d. -2
13. In a do-while loop, the Boolean expression is tested ______________.
a. before the loop is executed c. both before and after the loop is executed
b. after the loop is executed

14. In a while loop, the Boolean expression is tested ______________.


a. before the loop is executed c. both before and after the loop is executed
b. after the loop is executed

15. What is the output?


for (int i = 1; i < 15; i = i+3){
System.out.println(i);
}
a. 15 c. 47101316
b. 1471013 d. 147101516

16. How many times will the following code print “Welcome to Java”?
int count = 0;
while (count <=10){
System.out.println(“Welcome to Java”);
count++;
}
a. 9 c. 11
b. 10 d. 0

17. The statement x++; is equivalent to


a. x = x + x; c. x = x - 1;
b. x = x + 1; d. x = +1;

18. In a group of nested loops, which loop is executed the most number of times?
a. the outermost loop
b. the innermost loop
c. all loops are executed the same number of times
d. cannot be determined without knowing the size of the loops

19. What’s wrong? while( (i<10) && (i>24) )


a. the logical operator && cannot be used in a test condition
b. the while loop is an exit-condition
c. the test condition is always false
d. the test condition is always true

20. Which of the following loops prints “Java is cool.” 10 times?


a. for (int ctr = 1; ctr <=10; ctr++){
System.out.println(“Java is cool.”);
}
b. for (int ctr = 1; ctr <10; ctr++){
System.out.println(“Java is cool.”);
}
c. for (int ctr = 2; ctr <=10; ctr++){
System.out.println(“Java is cool.”);
}
d. for (int ctr = 0; ctr <=10; ctr++){
System.out.println(“Java is cool.”);
}

TEST II. TRUE OR FALSE. Write TRUE(uppercase) for True and FALSE(uppercase) for False.
Given the following Java codes are inside the public static void main() function.

21. The output from the following code is 45.


int sum = 0, cnt = 1;

while ( cnt < 10 ) {


sum += cnt;
cnt++;
}
System.out.println( sum );
22. The following code has no error.
cnt = 1;
while ( cnt <= 20 ) {
cnt = cnt -1;
}

23. The following while statement will never terminate.


int i = 1;

while ( i != 1000 ) {
if ( i % 2 == 0 ) {
i = i + 2;
}
else {
i = i - 1;
}
}

24. The following two while loops equivalent?


int cnt = 2; sum = 0;

while ( cnt < 100 ) {


cnt += 2;
sum ++;
}
System.out.println( sum );

------------------------------------

int cnt = 0; sum = 0;


while ( cnt <= 100 ) {
cnt += 2;
sum ++;
}
System.out.println( sum );

25. The following do-while statement has no syntax error.


int sum = 0;
do-while ( sum <= 10000 ) {
sum = sum + 10;
}

26. The following for statement will result in an infinite loop.


for (int i = 0; i != 99; i++ ) {
sum = sum + Math.sqrt( i );
i += 2;
}
27. The following is a valid for loop.
for (float x = 0; x < 100; x += 2.5; ) {
System.out.println( x );
}

28. The output from the following code is 20.


int sum = 0;

for ( int i = 0; i < 4; i++ ) {


sum +=i;
for ( int j = i; j < 4; j++ ) {
sum += j;
}
}
System.out.println( sum );

29. The control variable of a for loop must appear in , , and components of the for statement syntax.

30. The loop body of a do-while loop is executed 1 or more times while the loop body of a while loop is executed 0
or more times.
TEST IV. FINDING THE ERROR. The following Java Program is riddled with errors. Encircle all the errors you can find. Be
specific as possible as you can. The highest possible score is 15/10. Right minus Wrong.

/*
The following Java program is supposed to print a Pascal’s Triangle. The number of rows
will depend on the input from the user. The program keeps asking an input from the user
using while loop. If the user inputs 0, the program terminates. A sample output is
provided below.
*/

import java.util.scanner;

public class JavaPascalTriangle


{
public static void main(String args{});
{
Int r=0, i=0, k=0, number=1, j=0;
Scanner scan = new Scanner(System.In);

While(r !=0 ){
System.out.println("Enter Number of Rows : ");
r = in.nextInt();

If (r == 0);
break;
for(i=0;i<r;i++)
{
for(x=r; k>i; k++)
{
System.out.printf(" ");
}
num = 1;
for(j=0;j<=i;j++)
{
System.out.print(number+ " ");
number = number * (i - j) / (j + 1);
}
System.out.println()
}
}
}
}

This is the sample output:

TEST V. CODING: Create a complete Java Program for the following problem / situation. Write your answer at
the back. Please make your penmanship as readable as you can.

1. Display “Java is cool.” n times depending on user’s input. If the user inputs 4, display “Java rocks.” 4 times.
(5 points)
2. Display the numbers 0 to n depending on user’s input. If the user inputs 9, display the numbers 1 to 9.
(5 points)

You might also like