Professional Documents
Culture Documents
2. What is wrong with the following loop that prevents the printing to be executed?
int x = 1;
while (0 < x) && (x < 100)
System.out.println(x++);
a. The loop runs forever.
b. The code does not compile because the loop body is not in the braces.
c. The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of parentheses.
d. The numbers 1 to 99 are displayed.
Answer:. c
1 2 3 98 99
3. The below loop computes the following series: + + +…+ +
2 3 4 99 100
double sum=0;
for (double i=1;i<=99 ;i++)
sum+=i/(i+1);
Re-write the code using while loop
Answer:
double sum = 0;
double i =1;
while (i<100)
{
double formula = i / (i + 1);
sum+=formula;
i++;
}
System.out.println(sum);
}
}
4. Use the break command to oblige the following code to print numbers 10, 5, 6.
int balance = 10;
while (balance >= 1) {
System.out.println(balance);
balance = balance - 2;
}
1|Page
Spring 2019-2020
Answer:
a. (int)(Math.random() * 150 + 1)
b. (int)(Math.random() * 151)
c. (int)(Math.random() * 150)
d. (int)(Math.random() * 150) + 1
Answer:
b
6. The following code displays all numbers from 72 to 1000. Modify it in order to
display numbers from 72 to 1000 that are divisible by 5.
for(int i = 72; i <= 1000; i++)
System.out.print(i + " ");
Answer:
for(int i = 72; i <= 1000; i++)
{
int c = i%5;
if (c==0)
{
System.out.println(i+" ");
}
}
7. Update the following code to print the numbers from 1 to 5:
int i = 0;
while(i > =5)
i++;
System.out.print(i + " ");
Answer:
int i = 0;
while (i <5)
{
i++;
2|Page
Spring 2019-2020
8. The aim of the following program is to count all even numbers given by the user.
The program should display how many even numbers the user has entered. The code
written below displays the number of even integers for each new entry. How to
change this code in order to display the number of even integers only at the end?
Scanner input = new Scanner(System.in);
int count = 0;
for(int i = 0; i < 20; i++){
System.out.print("Enter a number: ");
int n = input.nextInt();
if(n % 2 == 0)
count++;
System.out.println("the number of even integers is " + count);
}
Answer:
Scanner input = new Scanner(System.in);
int count = 0;
for(int i = 0; i < 20; i++){
System.out.print("Enter a number: ");
int n = input.nextInt();
if(n % 2 == 0)
count++;
}
System.out.println("the number of even integers is " + count);
9. Consider the following code. What could be the input of the user in order to get the
following output (in order): 9 6 3. Give two possible inputs.
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int n = input.nextInt();
while (n > 0) {
int d = n % 10;
if (d % 3 == 0)
System.out.print(d + " ");
n /= 10;
}
Answer:
369 , 472369
10. The output of the following code is 0 1 2 3 4. Update the code in order to output 1
2 3 4 5. Do NOT change the value of i or the loop condition.
int i=0;
do{
System.out.println(i+" ");
i++;
}while(i<5);
Answer:
int i=0;
do{
i++;
System.out.println(i+" ");
}while(i<5);
3|Page
Spring 2019-2020
4|Page