Home »
        Java Aptitude Que. & Ans.
    
    Java conditional statements Aptitude Questions and Answers
    
    
        
    This section contains Aptitude Questions and Answers on Java Conditional Statements like if else, switch case statement, conditional statements etc.
    
    List of Java Conditional Statements Aptitude Questions
        
        1) 
        Consider the code snippet and select the correct output
        
if(true && false && true || false)
	System.out.println("True.");
else
	System.out.println("False");
        
            - True.
  
            - False.
  
        
        
        
           Correct Answer: 2
           False.
           In the statement && and || operators are used, priority of operator && is high. Below the execution steps:
           
                - true && false = false
 
                - false && true = false
 
                - false || false= false
 
           
           Finally, expression will return false.
         
    
      
        
        2) 
        What will be the output of following program?
        
public class temp
{
	public static void main(String args[])
	{
		int x=1;		
		if((boolean)x==true)
			System.out.println("True.");
		else
			System.out.println("False.");
		
	}
}
        
            - True.
  
            - False.
  
            - Error
  
        
        
        
        Correct Answer: 3
        Error
        Error "inconvertible types" will occur. Type mismatch problem cannot convert integer to boolean.
         
    
      
        
        3) 
        What will be the output of following program ?
        
         
public class temp
{
	public static void main(String args[])
	{
		int ok=10;
		switch(ok)
		{
			default:
				System.out.println("default");
			case 0:
				System.out.println("true");
			case 1:
				System.out.println("false");
		}
	}
}
        
            - default
 
            - Error
 
            - true false default
 
            - default true false
 
        
        
        
        Correct Answer: 4
        default true false
        Since switch falls top to bottom and there is no break statement used after default and case block, so default true false will print.
         
    
   
        
        4) 
        What will be the output of following program? 
        
public class temp
{
	public static void main(String args[])
	{
		boolean ok=true;
		switch(ok)
		{
			case true:
				System.out.println("true");
			break;
			case false:
				System.out.println("false");
			break;
			default:
				System.out.println("default");
			break;
		}		
	}
}
        
            - Error
 
            - true
 
            - false
 
            - default
 
        
        
        
        Correct Answer: 1
        Error
        Compilation time error "incompatible types" will occur, we cannot use boolean value with switch statement.
         
    
   
 
        
        5) 
        What will be the output of following program ? 
        
public class temp
{
	public static void main(String args[])
	{
		int x=1;	
		if(x)
			System.out.println("True");
		else
			System.out.println("False");
	}
}
        
            - True
 
            - False
 
            - Error
 
        
        
        
        Correct Answer: 3
        Error
        We cannot use an integer value within conditional expression; value must be whether true or false i.e. only boolean values are allowed.
         
    
   
    Page 2
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement