Wednesday, August 20, 2014

Java Programming Interview Questions

WHILE Loop

1. What is the output of the following code snippet?
int i=1,sum=0;
while(true){
sum+=i;
if(sum>14)
break;
if(i==5)
break;
    i=sum+sum;
}
System.out.println("Sum is :"+sum);

A. 14
B. 15
C. 27
D. 5
E. 28

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

A. 5,5
B. 6,5
C. 4,6
D. 5,5
E. 5,4

3. What is the output of the following code snippet?
int i=0,j=10;
do{
      i++;
--j;
}while(i<5);
System.out.println(i+","+j);

A. 5,7
B. 6,5
C. 4,6
D. 5,5
E. 5,4

4. What is the output of the following code snippet?
int num=9845;
while(num>9){
int sum=num%10+num/10;
num=sum;
}
System.out.println(num);

A. 9
B. 26
C. 0
D. 8
E. 7

5. What is the output of the following code snippet?
int num=98451;
while(num>9){
int sum=0;
while(num!=0){
sum+=num%10;
num=num/10;
}
num=sum;
}
System.out.println(num);

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

6. What is the output of the following code snippet?
int i=1,sum=0;
while(i<10){
  int j=1;
  int count=0;
    while(j<=i){
if(i%j==0){
  count++;
}
 j++;
}
       if(count==2){
if(i%2!=0)
          sum+=i;
      }
i++;
}
System.out.println(sum);

A. 17
B. 10
C. 15
D. 16
E. 19

7. What is the output of the following code snippet?
int i = 2, sum = 0;
while (true) {
   if (i % 2 == 0) {
sum += i;
   }
     i++;
            if(sum>9)
          break;
}
System.out.println(sum);

A. 9
B. 10
C. 11
D. 12
E. 13

8. What is the output of the following code snippet?
int count=0,i=1;
do{
count++;
i++;
}while(i<10);
System.out.println(count);

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

9. What is the output of the following code snippet?
int i=3;
while(2<3){
if(i<2)
break;
System.out.print(i--+" ");
}

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

10. What is the output of the following code snippet?
int i=3;
do{
  if(i<2)
     break;
  System.out.print(i--+" ");
}while(2<3);

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




Java Programming Interview Questions

SWITCH Statement

1. What is the output of the following code snippet?
public static void main(String[] args) {
int num=5;
 switch(num++){
     case 6:System.out.println("Num = "+6);
     case 5:System.out.println("Num = "+5);
     case 7:System.out.println("Num = "+7);
    break;
    default: System.out.println("Num = "+num);
}
}

A. Num = 6
B. Num = 5
C. Num = 5
Num = 6
D. Num = 5
Num = 7
E. Num = 5
Num = 6
Num = 7

2. What is the output of the following code snippet?
public static void main(String[] args) {
int num=6;
switch(++num){
case 6:System.out.println("Num = "+num);
      break;
case 5:System.out.println("Num = "+num);
      break;
case 7: System.out.println("Num = "+num);
default: System.out.println("Num = "+num);
}
}

A. Num = 5
B. Num = 7
C. Num = 6
D. Num = 6
Num = 6
E. Num = 7
Num = 7

3. What is the output of the following code snippet?
public static void main(String[] args) {
int num=9;
switch(++num){
default: System.out.println("Num = "+num);
case 4+3:System.out.println("Num = "+num);
        break;
case 4+5:System.out.println("Num = "+num);
break;
case 4+4: System.out.println("Num = "+num);
       break;
}
}

A. Num = 10
B. Num = 9
C. Num = 9
Num = 9
D. Num = 10
Num = 10
E. Num = 10
Num = 7

4. What is the output of the following code snippet?
public static void main(String[] args) {
for (int i = 3; i >= 0; --i) {
  switch (i) {
case 1:
System.out.println(i);
break;
case 2:
System.out.println(i);
break;
case 3:
System.out.println(i);
break;
default:System.out.println(i);
}

}
}

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

5. What is the output of the following code snippet?
public static void main(String[] args) {
float num = 12.5f;

  switch (num++) {
case 12.5f:
System.out.println("12.5");
break;
case 13.5f:
System.out.println("13.5");
break;
case 14.5f:
System.out.println("14.5");
break;
default:
System.out.println("Default");
  }
}

A. 12.5
B. 13.5
C. 14.5
D. Default
E. None of the above

6. What is the output of the following code snippet?
public static void main(String[] args) {
int num = 12;
switch (++num) {
case 12:
System.out.println("12");
case 13:
System.out.println("13");
case 14:
System.out.println("14");
return;
default:
System.out.println("Default");
}
}

A. 12
13
B. 13
14
C. 14
D. Default
E. None of the above

7. What is the output of the following code snippet?
public static void main(String[] args) {
int a = 99;
switch (a) {
case 'a':
System.out.println("a");
break;
case 'b':
System.out.println("b");
break;
case 'c':
System.out.println("c");
break;
default:
System.out.println("Default");

}
}

A. Default
B. b
C. C
D. a
E. none of the above

8. What is the output of the following code snippet?
public static void main(String[] args) {
char message[]={'t','h','e','q','u','i','c','k'};
int count1=0;
int count2=0;
for(int i=0;i<message.length;i++)
switch (message[i]) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':count1++;
default: count2++;
}
System.out.println(count1+ " "+count2);
}

A. 3 5
B. b. 4 4
C. 3 6
D. 3 7
E. 3 8

9. What is the output of the following code snippet?
public static void main(String[] args) {
int num=10;
switch(num++){
case 5+5: System.out.println("Expression");
break;
case 10: System.out.println("Value-10");
break;
case 11:System.out.println("Value-11");
break;
default: System.out.println("Default");
}
}

A. Expression
B. Value-10
C. c. Value-11
D. Default
E. None of the above

10. What is the output of the following code snippet?
public static void main(String[] args) {
int num=10;
switch(num++){
default: System.out.println("Default");
case 5+7: System.out.println("Expression");
break;
case 10: System.out.println("Value-10");
break;
case 11:System.out.println("Value-11");
break;

}
}

A. Default
B. Default
Expression
C. Value-10
D. Value-11
E. None of the above



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




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

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

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