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




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