Assignment 5 1.: Else Sum Sum + 23
Assignment 5 1.: Else Sum Sum + 23
1.
1 public class Q1
2 {
3 public static void main(String args[])
4 {
5 int x = 0, y =0;
6 int sum = 0;
7 while (x < 10){
8 y = x - 3;
9 y = 40;
10 while (y > 22){
11 if ((sum > 30) && (sum < 40))
12 sum = sum + x * 2 ;
13 else if ((sum > 40) && (sum < 50))
14 sum = sum + x * 3;
15 else
16 sum = sum + 23;
17 System.out.println(sum);
18 y = y - 10;
19 }
20 x += 2;
21 }
22 }
23 }
What are the values of sum that will be printed in line 11?
4. Consider the following code:
1 public class Quiz5
2 {
3 public static void main(String args[])
4 {
5 String test = "";
6 int i = 0, j = 0, k = 15;
7 while (i < 5){
8 test = "-->";
9 j = --k;
10 while (j > 10 ){
11 test = test + i + j;
12 System.out.println(test);
13 j--;
14 }
15 i++;
16 }
17 }
18 }
What are the values of test that will be printed in line 12?
5. Consider the following code:
1 public class Quiz5b
2 {
3 public static void main(String args[])
4 {
5 String test = "";
6 int i = 2, j = 0, k = 18;
7 while (i < 7){
8 test = "-->";
9 j = --k;
10 while (j > 13 ){
11 test = test + i + j;
12 System.out.println(test);
13 j--;
14 }
15 i++;
16 }
17 }
18 }
What are the values of test that will be printed in line 12?
6. State which of the followings are legal variable names in Java and which are not (write True for
legal and False for illegal).
_n(3)m
U.S.A.
amin Ullah
66Mohakhali
X23
Dr. Yunus
5 % 3
5.0 / 3
"23" + 3 + 4
4.0+2.0*(5.0+4.0/3.0)
4-(5+4/3)/3.0%2
7 % 3
7.0 / 3
"99" + 3 + 4
6.0+2.0*(5.0+4.0/3.0)
6-(5+4/3)/3.0%2
8. Consider the following code:
1 public class Q1
2 {
3 public static void main(String args[])
4 {
5 int x = 0, i =0, sum = 0;
6 i = 1;
7 x = 2;
8 sum = 0;
9 while (i < 20){
10 x = x + i;
11 sum = sum + x + 1;
12 System.out.println(sum);
13 if (x > 5)
14 i += 2;
15 else
16 i += 3;
17 }
18 sum = sum + i;
19 System.out.println(sum);
20 }
21 }
In the above program show the values that are going to be printed in line 12 and 19. (Lines with the println
statement).
1 public class Q2
2 {
3 public static void main(String args[])
4 {
5 int x = 0, y =0;
6 int sum = 0;
7 while (x < 10){
8 y = x - 3;
9 y = 40;
10 while (y > 22){
11 if ((sum > 30) && (sum < 40))
12 sum = sum + x * 2 ;
13 else if ((sum > 40) && (sum < 50))
14 sum = sum + x * 3;
15 else
16 sum = sum + 23;
17 System.out.println(sum);
18 y = y - 10;
19 }
20 x += 2;
21 }
22 }
23 }
In the above program show the values of sum that are going to be printed in the line 17.
10. consider the following code:
public class Q4
{
public static void main(String args[])
{
boolean var1 = false, var2 = false, var3 = false, var4 = false,var5
= false;
boolean var6 = false, result1 = false, result2 = false, result3 =
false, result4 = false;
boolean result5 = false, result6 = false, result7 = false, result8 =
false;
boolean result9 = false, result10 = false;
var1 = 4 > 3 - 1;
var2 = var1 && false;
var3 = true;
var4 = false;
var5 = true;
var6 = var3 && false;
result1 = (var1 || var2) && (8 * 10 > 45);
result2 = (var1 || var2) && (result1 && false);
result3 = (var1 && result1) || result2;
result4 = (var1 || var2) || ((var3 && var1) && false);
result5 = (var1 && var2) && (result3 || var1);
result6 = ((var3 || var2) && !(result5)) || true;
result7 = (var4 && result1) && ((result1 && false) || true);
result8 = ((var1 && result3) && (var5 || var6)) && true;
result9 = ((result2 && var2) || (result7 && var1)) && false;
result10 = !(var1 && true);
}
}
Show the values of the result variables in the above program:
result1
result2
result3
result4
result5
result6
result7
result8
result9
result10
11. consider the following code:
1 public class Q1
2 {
3 public static void main(String args[])
4 {
5 int x = 0, i =0, sum = 0;
6 i = 1;
7 x = 2;
8 sum = 0;
9 while (i < 20){
10 x = x + i;
11 sum = sum + x + 3;
12 System.out.println(sum);
13 if (x > 5)
14 i += 2;
15 else
16 i += 3;
17 }
18 sum = sum + i;
19 System.out.println(sum);
20 }
21 }
In the above program show the values that are going to be printed in line 12 and 19. (Lines with the println
statement).
1 public class Q2
2 {
3 public static void main(String args[])
4 {
5 int x = 0, y =0;
6 int sum = 0;
7 while (x < 10){
8 y = x - 3;
9 y = 40;
10 while (y > 22){
11 if ((sum > 30) && (sum < 40))
12 sum = sum + x * 3 ;
13 else if ((sum > 40) && (sum < 50))
14 sum = sum + x * 4;
15 else
16 sum = sum + 24;
17 System.out.println(sum);
18 y = y - 10;
19 }
20 x += 2;
21 }
22 }
23 }
In the above program show the values of sum that are going to be printed in the line 17.
13. consider the following code:
public class Q4
{
public static void main(String args[])
{
boolean var1 = false, var2 = false, var3 = false, var4 = false,var5
= false;
boolean var6 = false, result1 = false, result2 = false, result3 =
false, result4 = false;
boolean result5 = false, result6 = false, result7 = false, result8 =
false;
boolean result9 = false, result10 = false;
var1 = 4 < 3 - 1;
var2 = var1 && false;
var3 = false;
var4 = true;
var5 = false;
var6 = var3 && true;
result1 = (var1 || var2) && (8 * 10 > 45);
result2 = (var1 || var2) && (result1 && false);
result3 = (var1 && result1) || result2;
result4 = (var1 || var2) || ((var3 && var1) && false);
result5 = (var1 && var2) && (result3 || var1);
result6 = ((var3 || var2) && !(result5)) || true;
result7 = (var4 && result1) && ((result1 && false) || true);
result8 = ((var1 && result3) && (var5 || var6)) && true;
result9 = ((result2 && var2) || (result7 && var1)) && false;
result10 = !(var1 && true);
}
}
Show the values of the result variables in the above program:
result1 false
result2 false
result3 false
result4 false
result5 false
result6 true
result7 false
result8 false
result9 false
result10 true
14. Consider the following code:
1 public class Q2
2 {
3 public static void main(String args[])
4 {
5 int x = 0, y =0;
6 int sum = 0;
7 while (x < 10){
8 y = x - 3;
9 y = 40;
10 while (y > 22){
11 if ((sum > 30) && (sum < 40))
12 sum = sum + x * 2 ;
13 else if ((sum > 40) && (sum < 50))
14 sum = sum + x * 3;
15 else
16 sum = sum + 23;
17 System.out.println(sum);
18 y = y - 10;
19 }
20 x += 2;
21 }
22 }
23 }
In the above program show the values of sum that are going to be printed in the line 17.
1 public class Q2
2 {
3 public static void main(String args[])
4 {
5 int x = 0, y =0;
6 int sum = 0;
7 while (x < 10){
8
9 y = 40;
10 while (y > 22){
11 if ((sum >20) && (sum < 60))
12 sum = sum + x * 3 ;
13 }
else {
sum = sum + 23;
}
if ((sum > 60) && (sum < 100))
14 sum = sum + x * 4;
15 }
16
17 System.out.println(sum+y);
18 y = y - 10;
19 }
20 x += 2;
21 }
22 }
23 }
In the above program show the values of sum that are going to be printed in the line 17.
Code Output
String x = "cd";
int y = 21;
int p;
p = y % 3;
System.out.println(p + y / 2);
x = p + 1 + x + 4 + 1;
System.out.println(x);
int d = 23;
byte j = 22;
d = d / 2 * 3 + j;
System.out.println(d);
System.out.println(d / 2 +"21");
double d = 23;
int x = 3;
int c = 3;
x+= 8;
c = x % c;
System.out.println(c * 2);
d = 2 + d / c + 20;
System.out.println(d / 2 + 1);
26. Consider the following code:
Code Output
String x = "ab";
int y = 23;
int p;
p = y / 3 % 2;
System.out.println(y - p / 2);
x = p * 2 + x + (3 + 2);
System.out.println(x);
int d = 21;
byte j = 10;
j*=2;
d = d / 2 * 3 - j;
System.out.println(d);
System.out.println(d % 2 +"32"+ j);
double d = 42;
int x = 5;
int c = 2;
x+= 8;
d/=2;
c = x % c;
System.out.println(c * 2 + d);
d = 1 + d / c + 21;
System.out.println(d / 2 + 3 + "c");
Code Output
String x = "xz";
int y = 21;
int p;
p = y / 2 % 2;
System.out.println(y - p / 2);
x = p * 1 + x + (4 + 3);
System.out.println(x);
int d = 23;
byte j = 11;
j*=2;
d = d / 2 * 3 - j;
System.out.println(d);
System.out.println(d % 2 +"12"+ j);
double d = 22;
int x = 7;
int c = 2;
x+= 8;
d/=2;
c = x % c;
System.out.println(c * 3 + d);
d = 2 + d / c + 23;
System.out.println(d / 2 + 3 + "j");
30. Consider the following code:
public class Q4a
{
public static void main(String args[])
{
int x = 0, y =0;
int sum = 0;
double p;
while (x < 10){
y = x / 2;
while (y < x){
p = (x + 10.0) / 2;
sum = (sum % 2) + x - y * 2 + (int) p ;
System.out.println(sum);
y = y + 2;
}
if (x > 5){
x++;
}else{
x += 2;
}
}
}
}
What is the output of the program?
Code Output
String x = "abc";
int y = 27;
int p;
p = y % 3;
System.out.println(p + y / 2);
x = p + 2 + x + 3 + 2;
System.out.println(x);
int d = 27;
byte j = 20;
d = d / 2 * 3 + j;
System.out.println(d);
System.out.println(d / 2 +"42");
double d = 21;
int x = 13;
int c = 3;
x+= 7;
c = x % c;
System.out.println(c * 3);
d = 1 + d / c + 21;
System.out.println(d / 2 + 4);
Code Output
String x = "cde";
int y = 13;
int p;
p = y % 3;
System.out.println(p + y / 2);
x = p + 1 + x + 4 + 1;
System.out.println(x);
int d = 23;
byte j = 22;
d = d / 2 * 2 + j;
System.out.println(d);
System.out.println(d / 2 +"21");
double d = 23;
int x = 3;
int c = 3;
x+= 8;
c = x % c;
System.out.println(c * 3);
d = 2 + d / c + 20;
System.out.println(d / 2 + 5);
42. Consider the following code:
public class QuizA
{
public static void main(String args[])
{
String test = "";
int i = 0, j = 0, k = 15;
test = "<--cat";
while (i < 5){
k-=1;
j = k;
while (j > 10 ){
if (j % 2 == 0){
test += "-->";
test = test + i + (j / 2);
}else{
test += "<--";
test = test + (i / 2) + j;
}
System.out.println(test);
if (j == 12){
test = "<--cat";
}
--j;
}
i++;
}
}
}
What is the output of the program?