Wednesday, August 20, 2014

Java Programming Interview Questions

IF Statement
1. What is the output of the following code snippet?
void showResult() {
int a=10,b=20;
 if(a<b)
   System.out.println(a);
   System.out.println(b);
}

A. 10
B. 10
20
C. 20
D. 20
10
E. 10
10

2. What is the output of the following code snippet?
boolean a,b=false;
if(a=!(4==5)&&(b=true)){
 System.out.print(a +" ");
}
if(a=true && b){
 System.out.print(a);
}

A. true
B. true false
C. true true
D. false false
E. false true

3. What is the output of the following code snippet?
int sum=0;
for(int i=1;i<=20;i++){
if(i%4!=0)
        continue;
sum+=i;
}
System.out.println(sum);

A. 44
B. 56
C. 60
D. 64
E. 68
4. What is the output of the following code snippet?
void showResult() {
  boolean a=false,b=false,c=true;
if(a & (b=true) & (c=false));
System.out.println(a+" "+b+" "+c);
}

A. false false true
B. false false false
C. true true true
D. false true false
E. true true false

5. What is the output of the following code snippet?
void showResult() {
  boolean a=false,b=false,c=true;
if(a && (b=true) && (c=false));
System.out.println(a+" "+b+" "+c);
}

A. false false true
B. false false false
C. true true true
D. false true false
E. true true false

6. What is the output of the following code snippet?
int a=20,b=30,c=50;
 if(a<b && a<b)
System.out.println(a);
else if(b<c)
System.out.println(b);
else
System.out.println(c);

A. 50
B. 30
C. 20
D. No output
E. 0

7. What is the output of the following code snippet?
for(int i=1,j=5;i<=4;i++,j--){
if(i%j==0)
System.out.println(i+" "+j);
}

A. 2 3
4 4
B. 4 2
3 3
C. 3 3
4 2
D. 2 2
3 3
E. 4 4
3 3

8. What is the output of the following code snippet?
for(int i = 0; i < 2; i++) {
        for(int j = 2; j>= 0; j--) {
           if(i == j) break;
           System.out.print(i + " "+j+ " ");
        }
}

A. 0 2 0 1 1 1
B. 0 2 0 1 1 2
C. 1 2 0 1 1 2
D. 0 2 0 1 2 2
E. 0 2 1 0 0 2

9. What is the output of the following code snippet?
int count = 0;
for (int i = 0; i < 5; i++) {
for (int j = 2; j >= 0 ; j--, count++);
count++;
}
System.out.println(count);

A. 10
B. 15
C. 16
D. 20
E. 21

10. What is the output of the following code snippet?
int count = 0;
for (int i = 0; i < 5; i++) {
for (int j = 2; j >= 0 ; j--, count++){
count++;
}
}
System.out.println(count);

A. 25
B. 15
C. 16
D. 31
E. 30




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"...