Interview questions of static keyword in java and answers
(1)
(1)
class StaticDemo {
static int a=0;
int b=++a;
static int c=++a;
public static void main(String[] args){
System.out.print(c);
}
}
What will be output of above program?
(a)0
(b)1
(c)2
(d)Compiler error
Answer: (b)
(2)
class StaticDemo {
int a=0;
int b=++a;
static int c=++a;
public static void main(String[] args) throws Exception {
System.out.print(c^b);
}
}
What will be output of above program?
(a)0
(b)1
(c)2
(d)Compiler error
Answer: (d)
(3)
package manish;
class StaticDemo {
static {
StaticDemo {
System.out.println("raja");
}
catch(Exception e){
}
}
}
What will be output of above program?
(a)raja
(b)No main method in class exception.
(c)raja
No main method in class exception.
(d)Compiler error
Answer: (c)
(4)
class StaticDemo {
static {
System.out.println("raja");
}
public static void main(String[] args){
System.out.println("rani");
}
}
What will be output of above program?
(a)rani
raja
(b)raja
rani
(c)rani
(d)Compiler error
Answer: (b)
(5)
class StaticDemo {
static int a=0;
static {
System.out.println(a++);
}
public static void main(String[] args){
++a;
System.out.println(++a);
}
static {++a;}
}
What will be output of above program?
(a)1
3
(b)0
4
(c)2
2
(d)Compiler error
Answer: (b)
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
1 comment:
QNo:3 need to be corrected to get the given ans.
class StaticDemo {
static {
try
{
System.out.println("raja");
}
catch(Exception e){
}
}
}
Post a Comment