Conditional operator questions in java

Questions on conditional operators in java and answers

(1)

public class Conditional {
    public static void main(String[] args) {
         int a=5;
         Integer b=10;
         int c=++a>++b?++a:++a+b;
         System.out.print(c);
    }
}

What will be the output when you will compile and run the above code?

(a)16
(b)17
(c)18
(d)19





Answer: (c)

(2)

public class Conditional {
    public static void main(String[] args) {
         int a=5;
         short s=a>0?++a:~a;
         System.out.print(s);
    }
}


What will be the output when you will compile and run the above code?

(a)6
(b)-6
(c)5
(d)Compilation error





Answer: (d)

(3)

public class Conditional {
    public static void main(String[] args) {
         int a=5;
         long s=true==true?return ++a:return ~a+1;
         System.out.print(s);
    }
}

What will be the output when you will compile and run the above code?

(a)6
(b)-5
(c)Run time exception
(d)Compilation error





Answer: (d)

(4)

public class Conditional {
    public static void main(String[] args) {
         int a=5,b=10,c=8;
         long s=(a>b?a:b)>c?(a>b?a:b):c;
         System.out.print(s);
    }
}

What will be the output when you will compile and run the above code?

(a)5
(b)8
(c)10
(d)Compilation error



Answer: (c)

(5)

public class Conditional {
    public static void main(String[] args) {
         int a=5,b=10,c=8;
         long s=a>b?c:a>c?5.2%3:++b;
         System.out.print(s);
    }
}

What will be the output when you will compile and run the above code?

(a)8
(b)11
(c)1.5
(d)Compilation error





Answer: (d)

(6)

public class This {
    public static void main(String[] args) {
         int a=5,b=10,c=8;
         long s=a>b?c:a>c?b=+b++:++b;
         System.out.print(s);
    }
}

What will be the output when you will compile and run the above code?

(a)11
(b)21
(c)20
(d)Compilation error





Answer: (a)

(7)
  
public class This {
    public static void main(String[] args) {
         int a=5,b=10,c=8;
         double s=true?5F:"string";
         System.out.print(s);
    }
}

What will be the output when you will compile and run the above code?

(a)5
(b)null
(c)Run time exception
(d)Compilation error




Answer: (d)

(8)

public class This {
    public static void main(String[] args) {
         int a=5,b=10,c=8;
         double s=true?5D:void;
         System.out.print(s);
    }
}

What will be the output when you will compile and run the above code?

(a)5
(b)0
(c)NaN
(d)Compilation error





Post a Comment