Quiz 5: Java Programming Exercises - Prof. Erliang Zeng, Quizzes of Javascript programming

A java programming quiz with five questions. The quiz covers various topics such as for loops, while loops, do-while loops, and printing odd numbers using loops. Students are required to write the code to produce the desired output for each question.

Typology: Quizzes

Pre 2010

Uploaded on 03/11/2009

koofers-user-xrq-1
koofers-user-xrq-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz 5
Name: Panther ID:
Please use 15 minutes to finish this quiz.
1. Consider the following program
import java.util.Scanner;
public class Test {
public static void main (String args[])
{
final int LIMIT=5;
int sum=0;
for (int i=1; i<=LIMIT; i++)
sum=sum +i;
System.out.println(sum);
}
}
[2 pts] What is the output produce by the program?
15
[2 pts] Modify the program to use a while loop instead of the for loop
int i = 1;
while(i <= LIMIT)
{
//sum += i++;
sum += i;
i++;
}
[2 pts] Modify the program to use a do-while loop instead of the for loop
int i = 0;
do
{
sum = sum + i;
i ++;
}
while (i <= LIMIT)
2. [4 pts] Write a for loop to print the odd numbers from 1 to 99 separated by a tab
(inclusive).
for (int i = 1; i <= 99; i += 2)
System.out.println(i + “\t”);
pf2

Partial preview of the text

Download Quiz 5: Java Programming Exercises - Prof. Erliang Zeng and more Quizzes Javascript programming in PDF only on Docsity!

Quiz 5

Name: Panther ID: Please use 15 minutes to finish this quiz.

  1. Consider the following program import java.util.Scanner; public class Test { public static void main (String args[]) { final int LIMIT=5; int sum=0; for (int i=1; i<=LIMIT; i++) sum=sum +i; System.out.println(sum); } } [2 pts] What is the output produce by the program? 15 [2 pts] Modify the program to use a while loop instead of the for loop int i = 1; while(i <= LIMIT) { //sum += i++; sum += i; i++; } [2 pts] Modify the program to use a do-while loop instead of the for loop int i = 0; do { sum = sum + i; i ++; } while (i <= LIMIT)
  2. [4 pts] Write a for loop to print the odd numbers from 1 to 99 separated by a tab (inclusive). for (int i = 1; i <= 99; i += 2) System.out.println(i + “\t”);

Bonus [5 pts] Observe that the pattern below has 5 rows where each row i, has i columns containing values 1, 2, … i. Write a program that will print the following pattern: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 Write a program that will print the above pattern using nested loops. for (int i = 1; i <= 5; i++) { for(int j = 1; j <= i; j++) System.out.print(j + “\t”); System.out.println(“”); }