Wednesday, September 23, 2020

Apptitude Tests MCQ Short Questions On Loop BlueJ


In this page you will find short questions on BlueJ loop. These questions are MCQ  apptitude tests questions.

1. class LoopMCQ
{
    public void show()
    {
        for(int i = 0; i <= 5; i++ )
        {
            System.out.println("i = " + i );
        }
        System.out.println("End of Loop = " + i );
    }
}


A. i = 0
   i = 1
   i = 2
   i = 3
   i = 4
   i = 5
   End of Loop = 6

B. i = 0
   i = 1
   i = 2
   i = 3
   i = 4
   i = 5
   i = 6

C. Compilation Errors

D. i = 0
   i = 1
   i = 2
   i = 3
   i = 4
   i = 5

    Correct Answer: C. 'i' is declared in the for loop so scope and lifetime of the variable is within that loop only.


2.class LoopMCQ
{

    public void show()
    {
        int i = 0;
        for(;i <= 5; i++ )
        {
            System.out.println("i = " + i );
        }

        System.out.println("End of Loop = " + i );
    }
}

A.   i = 0
      i = 1
      i = 2
      i = 3
      i = 4
      i = 5
     End of Loop = 6

B. i = 0
     i = 1
     i = 2
    i = 3
    i = 4
    i = 5
    i = 6

C. Compilation Errors

D. i = 0
     i = 1
     i = 2
     i = 3
     i = 4
     i = 5

 Correct Answer: A. 

3. class LoopMCQ
{

    public void show()
    {
        int a, b;
        for(a = 1, b = 4; a < b; a++, b--)
        {
            System.out.println("a = " + a);
            System.out.println("b = " + b);
        }
    }
}

A. Compilation error 

B. a = 1
     b = 4
     a = 2
     b = 3
     a = 3
     b = 2

C. a = 1
    b = 4
    a = 2
    b = 3

D. Run Time Error

Correct Answer: C. when 'a' becomes 3 and 'b' becomes 2, the loop terminates.

4. class LoopMCQ
{
    public void show()
{
    int i=0;
    for(;;)
    {
        if(i==10)
            break;
        System.out.print(++i);
    }
    
}
}

A. 0 1 2 3 4 5 6 7 8 9 10
B.  0 1 2 3 ... infinite times
C.  1 2 3 4 5 6 7 8 9 10
D.  1 2 3 4 5 6 7 8 9

Correct Answer: C. Prefix operator is used within System.out.print() method , so value of 'i' get incremented before display. Initial value of 'i' is 0, so the display starts from 1 and when the value of 'i' is 9, the loop displays 10 (prefix operator) and in the next iteration, loop terminates.

5. class LoopMCQ
{
    public void show()
{
    int i;
    for(i=0;i<10;++i)
    {
        System.out.print("#");
        if(i>6)
            continue;
        System.out.print(i);
    }
}   

A. #0#1#2#3#4#5#6###
B. #0#1#2#3#4#5#6#7#8#9#10
C. #0#1#2#3#4#5##7#8#9#10
D. #0#1#2#3#4#5#*/

Correct Answer: A. The loop displays both '#' and '0' until 'i' becomes 7. For the values 7,8 and 9 of 'i', only the '#' symbol is displayed as contunes statement sends the control to re-initialisation statement of the loop.

6. class LoopMCQ
{
    public void show()
{
    int i,j;
    char ch='A';
 
    for(i=5;i>=1;i--)
    {
        for(j=0;j< i;j++)
          System.out.print((char)(ch+j));
        System.out.println();
    }
}
}

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

 Correct Answer: 4. Outer loop executes for 5 times and on each iteration of outer loop, one new line is feed. So, answers may be 1 or 4. Inner loop's iteration is gradually decreased with the iteration of outer loop. So the answer is 4.


7. class LoopMCQ
{
    public void show()
{
    int i=1;
    while(i>=10)
    {
        System.out.print(i);
        i+=1;
    }
            System.out.print("End of Loop"+i);
           System.out.println();
}
}

A. After loop i=1
B. 1,
C. After loop i=2
D. After loop i=2

Correct Answer: A. Loop will not be executed for a single time. Only display and that is from outside the loop.

8. class LoopMCQ
{
public void show()
{
int i = 1, j = 1;
for(--i , j++ ; i<10; i+=2)
{
System.out.print(i+" ");
}
}
}

A. Compilation error

B. Program never ends

C. 0 2 4 6 8

D. None of the above

Correct Answer: C.  'i' starts the loop with value 0 and gradually increases by step 2.

Output of following


1. for(i=1;i<=5;i++)
{
 System.out.println(i+ ++i);
}

12
34
56

2. int a=2,i
for(i=8;i>=1;i--)
{
 System.out.println(i+a);
i-=a
}
82
52
22

3. for(i=1;;i+=2)
{
if(i<=6)
 System.out.println(i*2);
else
break;
}

2
6
10


4. for(i=2;i<=8;i*=2)
System.out.println(i);
System.out.println(i*2);

2
4
8
32

5. for(char a=’A’=2;a<=’E’;a++)
System.out.println((char)a+1);

B
C
D
E
F


6. for(i=10;i>=1;i--)
{

System.out.println(i);
if(i<6)
break
}
10
9
8
7
6
5


7. for(i=1;i<=10;i++)
{

if(i==7)
continue;
else
System.out.print(i);
}

1 2 3 4 5 6 8 9 10


8. for(i=10;;i--)
{

if(i==0)
break;
System.out.print(i+ “ “);
}

10  9  8  7  6  5  4  3  2  1


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner