Wednesday, August 20, 2014

Java Programming Interview Questions


FOR Loop
1. What is the output of the following code snippet?
for(int i=1;i<=3;i++){
System.out.print(i++);
}

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

2. What is the output of the following code snippet?
int i;
for(i=1;i<=3;i++);
System.out.print(i+" ");

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

3. What is the output of the following code snippet?
int i;
for(i=1;i<=3;i++);
System.out.print(i+" ");

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

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

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

5. What is the output of the following code snippet?
int n=10,i,j;
for(i=1;i<=n;i++){
  int count=0;
for( j=1;j<=i;j++){
if(i%j==0)
count++;
}
       if(count==2)
   if(i%2!=0)
System.out.print(i+" ");
}

A. 2 3 5 7 9 
B. 3 5 7 9 
C. 3 5 7
D. 1 3 5 7 9
E. 1 2 3 5 7 9

6. What is the output of the following code snippet?
int i=0,j=1;
System.out.print(i+" "+j);
for(int k=1;k<=3;k++){
int sum=i+j;
        System.out.print(" "+sum);
i=j;
j=sum;
}

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

7. What is the output of the following code snippet?
int k=1;
for(int i=1;i<3;i++){
for(int j=1;j<=3;j++){
System.out.print(k+++" ");
}
System.out.println();
}

A. 1 2 3
1 2 3
B. 1 2 3 
3 2 1
C. 1 2 3
4 5 6
D. 1 2 3
2 3 4 
E. None of the above

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

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

9. What is the output of the following code snippet?
int i, j, k, l;
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3 - i; j++)
System.out.print(" ");
for (k = 1; k <= i; k++)
System.out.print(k);
for (l = k - 2; l >= 1; l--)
System.out.print(l);
System.out.println();
}

A. 1 
123
1232
B. 1 
123
12345
C. 1
121
12321
D. 1
121
123321
E. None of the above

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

A. 8
B. 7
C. 9
D. 10
E. 11

11. What is the output of the following code snippet?
int i,j,count=0;
for(i=1;i<=3;i++){
  for(j=1;j<=3;j++,count++);
}
System.out.println(count);

A. 8
B. 7
C. 9
D. 10
E. 11

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