You are on page 1of 5

Class Work Week 3

Q1. What is the output?


int num = 24;
if (num >= 20) {
System.out.println("The number greater than 20.");
System.out.println("If block executed.");
} else {
System.out.println("The number is less than 20");
System.out.println("Else block executed.");
}

THE NUMBER IS GREATER THAN 20


Q2. What is the output?
int num = 0;

if (num > 20) {


System.out.println("If block executed.");
System.out.println("The number is greater than 20.");
} else if (num == 20) {
System.out.println("First Else if block executed.");
System.out.println("The number is equal to 20.");
} else if (num != 0) {
System.out.println("Second else if block executed.");
System.out.println("The number is not equal to 0.");
} else {
System.out.println("Else block executed.");
System.out.println("The number is = " + num);
}

Else block executed.


The number is = 0
Q3. What is the output?
String name = "Raidas";
switch(name) {
case "Meera" :
System.out.println("name is Meera.");
break;
case "Kabir" :
System.out.println("Name is Kabir");
break;
case "Raidas" :
System.out.println("Name is Raidas");
break;
default :
System.out.println("The default name is Ram");
}
Name is Raidas
Q4. What is the output?
String name = "Kabir";
switch(name) {
case "Meera" :
System.out.println("name is Meera.");
break;
case "Kabir" :
System.out.println("Name is Kabir");
case "Raidas" :
System.out.println("Name is Raidas");
default :
System.out.println("The default name is Ram");
}
Name is Kabir
Name is Raidas
The default name is Ram
Q5. How many lines of output? What is the output?
for(int i=0 ; i<=4 ;i++){
System.out.println("Increment Counter Value is i: "+i);
}
for(int j=5 ; j>=1 ;j-){
System.out.println("Decrement Counter Value is : "+j);
}

Increment Counter Value is i: 0


Increment Counter Value is i: 1
Increment Counter Value is i: 2
Increment Counter Value is i: 3
Increment Counter Value is i: 4
Decrement Counter Value is : 5
Decrement Counter Value is : 4
Decrement Counter Value is : 3
Decrement Counter Value is : 2
Decrement Counter Value is : 1

Q6. How many lines of output? What is the output?

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


System.out.println("Increment Counter Value is : "+i);
for(int j=3 ; j>=1 ;j--){
System.out.println("Decrement Counter Value is : "+j);
}
}
Increment Counter Value is : 0
Decrement Counter Value is : 3
Decrement Counter Value is : 2
Decrement Counter Value is : 1
Increment Counter Value is : 1
Decrement Counter Value is : 3
Decrement Counter Value is : 2
Decrement Counter Value is : 1
Increment Counter Value is : 2
Decrement Counter Value is : 3
Decrement Counter Value is : 2
Decrement Counter Value is : 1
Increment Counter Value is : 3
Decrement Counter Value is : 3
Decrement Counter Value is : 2
Decrement Counter Value is : 1
Increment Counter Value is : 4
Decrement Counter Value is : 3
Decrement Counter Value is : 2
Decrement Counter Value is : 1

Q7. Rewrite Q5 using while Statement


for(int i=0 ; i<=4 ;i++){
System.out.println("Increment Counter Value is : "+i);
}
for(int j=5 ; j>=1 ;j--){
System.out.println("Decrement Counter Value is : "+j);

int i=0;
while(i<=4) {
System.out.println("Increment Counter Value is : "+i);
i++;
}
int j=5;
while(j>=1);
{
System.out.println("Decrement Counter Value is : "+j);
j--;
}
}

You might also like