Test Series - java

Test Number 32/64

Q: What is the output of the Java program?
String name1="FOX", name2="DOG";
if(name1 == "FOX")
  System.out.print("FOX ");
  System.out.println("GOOD");					
if(name2 == "CAT")
  System.out.println("DINO");
A. FOX DINO
B. FOX GOOD DINO
C. Compiler error
D. None
Solution: The second Print statement "GOOD" is always executed.
Q: What is the output of the Java program?
String name="dino";
if(name == "dino")
	System.out.print("DINO");
System.out.println("GOOD");	
A. DINO GOOD
B. DINO
C. GOOD
D. Compiler error
Solution: The second Print statement is not part of the IF statement. So it is always executed.
Q: What is the output of the Java program with IF-ELSE-IF statements?
int marks=55;
if(marks >= 80)
  System.out.println("DISTINCTION");
else if(marks >=35)
  System.out.println("PASS");
else
  System.out.println("FAIL");
A. DISTINCTION
B. PASS
C. FAIL
D. Compiler error
Solution: no solution
Q: What is is the output of the Java program?
int marks=85;
if(marks >= 80)
  System.out.println("DISTINCTION");
else if(marks >=35)
  System.out.println("PASS");
A. DISTINCTION
B. PASS
C. Compiler error
D. None
Solution: It is ok to skip the ELSE statement.
Q: What is the output of Java program below?
float temp = 98.4f;
if(temp > 98.4)
{
  System.out.println("SUMMER");
}
else
{
  System.out.println("UNKNOWN");
}
A. SUMMER
B. UNKNOWN
C. Compiler error
D. None
Solution: no solution
Q: What is the output of the Java program?
long num = 123L;
if(num > 123)
{
	System.out.println("TIGER");
}
else
{
	System.out.println("BIRD");
}
A. TIGER
B. BIRD
C. Compiler error
D. None
Solution: no solution
Q: What is the output of the Java program?
int horses = 10;
int camels = 5;
if(horses > 5)
{
  if(camels > 3)
  {
    System.out.println("FOREST");
  }
}
else
{
   System.out.println("CITY");
}
A. FOREST
B. CITY
C. Compiler error
D. None
Solution: Nesting of IF and ELSE is allowed in Java.
Q: What is the output of the Java program?
int horses = 10;
int camels = 5;
if(horses < 5)
{
  System.out.println("TOWN");
}
else if(horses >=5)
  System.out.print("FOREST ");
  System.out.println("AMAZON");
else
  System.out.println("UNKNOWN");
A. TOWN
B. FOREST AMAZON
C. UNKNOWN
D. Compiler error
Solution: ELSE-IF statement should not contain more than one statement without braces { }.
Q: What is the output of the Java program?
int marks=29;
if(marks > 29);
   System.out.print("PASS ");
System.out.println("RANK");
A. RANK
B. PASS
C. PASS RANK
D. Compiler error
Solution: Notice the immediate Semicolon (;) after the IF. It ends the IF block. So whatever is next or below it will be executed always.
Q: What is the output of the Java program below?
if(3>1)
{
  4;
}
A. 0
B. 4
C. Compiler error
D. None
Solution: 4; is not a valid statement.

You Have Score    /10