Review sheet for Quiz Three Chapter Four-Loops: 1.What w be the values of x and
ID: 3725642 • Letter: R
Question
Review sheet for Quiz Three Chapter Four-Loops: 1.What w be the values of x and y as a result of the following code? int x 25, y 8: a x-25,y-8 b. x 33, y-8 cx-33, y-9 d. x 34, y-9 2. What will be the value of x after the following code is executed? int x, y-4,z- 6 a. 24 b. 28 C. 30 d. 35 3. What will be the value of x after the following code is executed? int x 10 while (x 100) x10i a. 90 b. 100 c. 110 d. This is an infinite loop 4. What will be the value of x after the following code is executed? int x 10, y 20: while (y100) a. 90 Review for QB: Page 1Explanation / Answer
1)c
2)b
3)This is an infinite loop
d)
2 4
3 13
4 29
5 54
_______
e)
AAA
BBB
CCC
DDD
_______
f)
1
22
333
4444
_______
g)
Sum = 6
_______
h)
Sum = 11
_______
15)
public class Demo {
public static void main(String[] args) {
int sum=0;
for(int i=1;i<=50;i++)
sum+=i;
System.out.println("Sum = "+sum);
}
}
______________
Output:
Sum = 1275
______________
16)
for(int i=1;i<=20;i++)
System.out.println("Hello World");
______________
17)
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int sumSales=0,val;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
while(true)
{
//Getting the input entered by the user
System.out.print("Enter Sales (-1 to exit):");
val=sc.nextInt();
if(val==-1)
break;
else
sumSales+=val;
}
System.out.println("Sum of Sales :"+sumSales);
}
}
______________________
Output:
Enter Sales (-1 to exit):78
Enter Sales (-1 to exit):76
Enter Sales (-1 to exit):65
Enter Sales (-1 to exit):54
Enter Sales (-1 to exit):43
Enter Sales (-1 to exit):-1
Sum of Sales :316
______________________
18)
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
int count = 0, val;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
while (true) {
// Getting the input entered by the user
System.out.print("Enter Positive number (-1 to exit):");
val = sc.nextInt();
if (val == -1)
break;
else if (val <= 0) {
continue;
}
else {
if (val % 2 == 0)
count++;
}
}
System.out.println("No of Even Integers :" + count);
}
}
_________________
Output:
Enter Positive number (-1 to exit):56
Enter Positive number (-1 to exit):54
Enter Positive number (-1 to exit):43
Enter Positive number (-1 to exit):32
Enter Positive number (-1 to exit):22
Enter Positive number (-1 to exit):33
Enter Positive number (-1 to exit):44
Enter Positive number (-1 to exit):55
Enter Positive number (-1 to exit):78
Enter Positive number (-1 to exit):89
Enter Positive number (-1 to exit):10
Enter Positive number (-1 to exit):-1
Sum of Sales :7
_________________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.