Wednesday, August 20, 2014

Java Programming Interview Questions


Basic Declarations
1. What is the output of the following code snippet?
public class Demo {
static int i=100;
public static void main(String[] args) {
final int i=1;
int j=999;
int k=i+j;
System.out.println(i+" "+ j+" "+k);
}
}

A. Compilation error
B. 100 999 1099
C. C. 1   999 1000
D. 1009991000
E. Final variables can't be declared in the method

2. What happens when the following code snippet is compiled and run?
protected class Demo {
public static void main(String[] args) {
System.out.println(1+2+" = "+2+1);
}
}

A. 3  = 3
B. 3  = 2+1
C. 12 = 21
D. 3 = 21
E. None of the above

3. What is the output of the following code snippet?
public class Demo {
public static void main(String[] args) {
int a;
System.out.println(a++ +" "+a++);
}
}

A. 0 1
B. 1 2
C. 2 2 
D. 1 1
E. None of the above

4. What is the output of the following code snippet?
public class Demo {
static int i=99;
static{
int i=100;
i++;
}
public static void main(String[] args) {
System.out.println(++i);
}
}

A. 99
B. 100
C. 101
D. 102
E. None of the above

5. Which of the following are valid declarations of the main method?

A. public static void Main(String args[])
B. public static void main(String args[])
C. public static void main(String... args)
D. public final static void main(String... args)
E. public static final void main(String args[])

6. What is the output of the following code snippet?
public class Demo {
public static int increment(int num){
System.out.println(num);
return num++;
}
public static void main(String[] args) {
int num=99;
increment(num);
System.out.println(num);
}
}

A. 99
100
B. 99
101
C. 100
101
D. 99
99
E. 100
100

7. What is the output of the following code snippet?
private class Demo {
public static void main(String[] args) {
int num=99;
num++;
++num;
System.out.println(num);
}
}

A. 100
B. 101
C. 102
D. 103
E. None of the above

8. What is the output of the following code snippet?
public class Demo {
public static void main(String[] args) {
final int num=99;
System.out.println(num+1);
System.out.println(num*2);
System.out.println(num);
}
}

A. Compilation error
B. 100
200
99
C. 100
200
200
D. 100
198
99
E. None of the above

9. What is the output of the following code snippet?
public class Demo {
public static void main(String[] args) {
final int num=99;
System.out.println(num+2);
num-2;
System.out.println(num);
}
}

A. 101 
99
B. 99
100
C. 100
101
D. 101
102
E. None of the above

10. What is the output of the following code snippet?
public class Demo {
public final static void main(String[] args) {
int j=10;
{
int i=10;
j++;
System.out.println(++i);
}
System.out.println(j);
}
}

A. 10
10
B. 11
11
C. 10
11
D. 11
10
E. None of the above

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...