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
Answer: (d)
Java questions of data types and answer
Java questions on if else statements
Java questions on switch case and answers
Java questions on looping and answers
Java questions on characters
Java questions on strings and answers
Java questions on variables and answers
Java questions on automatic type promotions
Java questions on bit wise operator
Java questions on operators and answers
Java questions on static keyword and answers
Java questions on super keyword and answers
Java questions on abstract class an answers
Java questions on interface and answers
Java questions on enum data type and answers
Java questions on break and continue keyword
Java questions of primitive data types
Java questions on conditional operators
Java questions on two dimensional array and answers
Java questions of data types and answer
Java questions on if else statements
Java questions on switch case and answers
Java questions on looping and answers
Java questions on characters
Java questions on strings and answers
Java questions on variables and answers
Java questions on automatic type promotions
Java questions on bit wise operator
Java questions on operators and answers
Java questions on static keyword and answers
Java questions on super keyword and answers
Java questions on abstract class an answers
Java questions on interface and answers
Java questions on enum data type and answers
Java questions on break and continue keyword
Java questions of primitive data types
Java questions on conditional operators
Java questions on two dimensional array and answers
No comments:
Post a Comment