1. What will be the output of following JAVA code?
public class If_1
{
public static void main(String args[])
{
String a="Learning JAVA_IF";
if ("Learn with Durga".contains("earn"))
System.out.println(a);
else
System.out.println("Never stop Learn");
}
}
Output:
Learning JAVA_IF
Explanation:
If condition checks the string in-built method "Learn with Durga" has sub-string "earn" and it returns true; If statement satisfies true part and it prints value of a.
2. What will be the output of following JAVA code?
int a=5;
if(a<0){
if (a<5)
System.out.println(a);
else
System.out.println(++a);
}
System.out.println(a++);
}
}
Output:
5
Explanation:
Outer if statement fails the condition, so it comes out of the entire if scope and executes SOP statement outside of the scope.
3. What will be the output of following JAVA code?
static boolean bo;
public static void main(String args[])
{
int b=87;
if (bo)
System.out.println(b);
else
System.out.println(++b);
}
}
Output:
88
Explanation:
Boolean variable bo is an instance variable of class If_3 and it initializes the default value of boolean (false). If statement checks the boolean value and prints else part value ++b (b=88).
4. What will be the output of following JAVA code?
public class If_4
{
public static void main(String args[])
{
boolean x = true;
boolean y = false;
if (x && y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
Output:
false
Explanation:
If condition checks logical AND operation of x and y and it returns false, prints false.
Note: Logical AND doesn't checks the second part of operation if the first part is false.
5. What will be the output of the JAVA code, when choice is 1?
public class Switch_1
{
public static void main(String args[])
{
int choice=1;
switch (choice){
case '1':System.out.print("Learn");break;
case '2':System.out.print("with");break;
case '3':System.out.print("Durga");break;
default:System.out.print("keep learning");
}
}
}
Output:
Keep learning
Explanation:
Integer variable choice doesn't matches with the cases inside switch; So, it prints default statement.
6. What will be the output of the following JAVA code?
public class Switch_2
{
public static void main(String args[]){
int print= 3;
switch (print){
case 1:case 3:case 5: System.out.print("Odd I am"); break;
case 2:case 4:case 6:System.out.print("Even I am"); break;
default:System.out.print("Neither odd nor even");
}
}
}
Output:
Odd I am
Explanation:
Switch statement supports nested case statement, make sure that same case doesn't have different definition in another nested case.
7. What will be the output of following JAVA code?
public static void main(String args[])
{
int pick=14;
switch (pick){
case 7+7:System.out.print("Learn ");
case 8+7:System.out.print("with ");
case 9+7:System.out.print("Durga ");
default:System.out.print("keep learning");
}
}
}
Output:
Learn with Durga keep learning
Explanation:
Switch case supports constant expression and it doesn't allow neither variable nor variable expression.
public class If_1
{
public static void main(String args[])
{
String a="Learning JAVA_IF";
if ("Learn with Durga".contains("earn"))
System.out.println(a);
else
System.out.println("Never stop Learn");
}
}
Output:
Learning JAVA_IF
Explanation:
If condition checks the string in-built method "Learn with Durga" has sub-string "earn" and it returns true; If statement satisfies true part and it prints value of a.
2. What will be the output of following JAVA code?
public class If_2{
public static void main(String args[]){int a=5;
if(a<0){
if (a<5)
System.out.println(a);
else
System.out.println(++a);
}
System.out.println(a++);
}
}
Output:
5
Explanation:
Outer if statement fails the condition, so it comes out of the entire if scope and executes SOP statement outside of the scope.
3. What will be the output of following JAVA code?
public class If_3
{ static boolean bo;
public static void main(String args[])
{
int b=87;
if (bo)
System.out.println(b);
else
System.out.println(++b);
}
}
Output:
88
Explanation:
Boolean variable bo is an instance variable of class If_3 and it initializes the default value of boolean (false). If statement checks the boolean value and prints else part value ++b (b=88).
4. What will be the output of following JAVA code?
public class If_4
{
public static void main(String args[])
{
boolean x = true;
boolean y = false;
if (x && y) {
System.out.println(true);
} else {
System.out.println(false);
}
}
}
Output:
false
Explanation:
If condition checks logical AND operation of x and y and it returns false, prints false.
Note: Logical AND doesn't checks the second part of operation if the first part is false.
5. What will be the output of the JAVA code, when choice is 1?
public class Switch_1
{
public static void main(String args[])
{
int choice=1;
switch (choice){
case '1':System.out.print("Learn");break;
case '2':System.out.print("with");break;
case '3':System.out.print("Durga");break;
default:System.out.print("keep learning");
}
}
}
Output:
Keep learning
Explanation:
Integer variable choice doesn't matches with the cases inside switch; So, it prints default statement.
6. What will be the output of the following JAVA code?
public class Switch_2
{
public static void main(String args[]){
int print= 3;
switch (print){
case 1:case 3:case 5: System.out.print("Odd I am"); break;
case 2:case 4:case 6:System.out.print("Even I am"); break;
default:System.out.print("Neither odd nor even");
}
}
}
Output:
Odd I am
Explanation:
Switch statement supports nested case statement, make sure that same case doesn't have different definition in another nested case.
7. What will be the output of following JAVA code?
public class Switch_3
{public static void main(String args[])
{
int pick=14;
switch (pick){
case 7+7:System.out.print("Learn ");
case 8+7:System.out.print("with ");
case 9+7:System.out.print("Durga ");
default:System.out.print("keep learning");
}
}
}
Output:
Learn with Durga keep learning
Explanation:
Switch case supports constant expression and it doesn't allow neither variable nor variable expression.
🔙
🔜
No comments:
Post a Comment