Test Series - java

Test Number 37/64

Q: Choose the Java-Code below with a never-ending loop.
A. while(true);
B. for(;true;);
C. do { ; }while(true);
D. All
Solution: no solution
Q: A loop in Java generally contains a Loop-Counter variable. State TRUE or FALSE.
A. FALSE
B. TRUE
C. None
D. blank
Solution: no solution
Q: An Increment operator "++" and/or a Decrement operator "--" are used along with a Loop-Counter variable in Java. (TRUE / FALSE).
A. FALSE
B. TRUE
C. None
D. blank
Solution: no solution
Q: What is the output of the below Java program?
int a=1;
while(a<4)
{
  System.out.print(a + " ");
  a++;
}
A. 1 2 3 4
B. 1 2 3
C. 6
D. Compiler error
Solution: a++; yields to a=a+1;
Q: What is the output of the below Java program with a decrement operator and WHILE-loop?
int a=4;
while(a>0)
{
 System.out.print(a + " ");
 a--;
}
A. 4 3 2 1
B. 3 2 1
C. Compiler error
D. None
Solution: no solution
Q: What is the output of the below Java program?
String str="FOX";
int i=0;
while(i
A. FFF
B. FOX
C. Compiler error
D. None
Solution: no solution
Q: What is the output of the below Java program with WHILE, BREAK and CONTINUE?
int cnt=0;
while(true)
{
  if(cnt > 4)
    break;
  if(cnt==0)
  {	
    cnt++;
    continue;
  }
  System.out.print(cnt + ",");
  cnt++;
}
A. 0,1,2,3,4,
B. 1,2,3,4,
C. 1,2,3,4
D. Compiler error
Solution: CONTINUE takes the program execution to the beginning of the loop by skipping the present iteration.
Q: What is the main difference between a WHILE and a DO-WHILE loop in Java?
A. WHILE loop executes the statements inside of it at least once even if the condition is false.
B. DO-WHILE loop executes the statements inside of it at least once even if the condition is false.
C. WHILE loop is fast.
D. DO-WHILE loop is fast.
Solution: Both the WHILE loop and DO-WHILE loop work at the same speed. A DO-WHILE loop executes the statements inside of it even the condition is false. It is the reason why a DO-WHILE loop is used in MENU driven console java programs.
Q: What is the value of "age" in the below Java program with a DO-WHILE loop?
int age=20;
do
{
  age++;
}while(age<20);
System.out.println(age);
A. 20
B. 21
C. Compiler error
D. None
Solution: WHILE condition fails. By that time, the increment statement was executed for one time. So its new value is 21.
Q: What is the output of the below java program that implements nesting of loops?
int i=1, j=1;
while(i<3)
{
  do
  {
    System.out.print(j + ",");
    j++;
  }while(j<4);
  i++;
}
A. 1,2,3,4,1,2,3,4,
B. 1,2,3,4,
C. 1,2,3,1,2,3,
D. 1,2,3,
Solution: Do-WHILE works for the first time printing (1,2,3). In the next iteration of i=2, the value of j is already 4. So it is printed and checked for condition (j<4). The inner loop exits. In the third iteration with i=3, nothing is printed.

You Have Score    /10