Thursday, July 15, 2021

Java Quiz-5(for loop)

Java Multiple Choice Questions(for loop)


1. What is the output of the below Java code with a FOR loop?

for(int i=1; i<5; i++)
{
  System.out.print(i +",");
}
A.1,2,3,4,
B.1,2,3,4
C.1,2,3,4,5
D.none


2. for is ______________ statement in java.

A.branching
B.iteration or looping
C.decision-making
D.all


3. Which statement is Incorrect?

A.for loop must be used when the number of iteration is fixed, it is recommended to use for loop.
B.for loop must be used when the number of iteration is not fixed, it is recommended to use for loop.
C.| for(init;condition;incr/decr){ // code to be executed } | is for loop syntax
D.for loop is a control flow statement that iterates a part of the program multiple times


4. What is not a type of Java for-statement?

A.Simple for-statement
B.for-each statement or extended for-statement
C.for-else statement
D.Labeled for statement


5. Which statements are true about labeled for-statement?

A.We can provide a label to a for-loop to identify uniquely when multiple loops are used in nested formation.
B.Labeling is generally used with break and continue statements.
C.Both A & B
D.Labels are just to decorate for-statements


6. What will be the output of the following program?

public class Main {

	public static void main(String[] args){

		for(int i = 0 ; i < 10; i++){			
		}		
		System.out.println(i);
	}
}
A.9
B.10
C.11
D.Compilation Error


7. What will be the output of the following program?

public static void main(String[] args){
		
		int i = 0;
		for(; i < 10; i++){
			break;
		}
		System.out.println(i);		
	}
}
A.0
B.1
C.10
D.Compilation error


8. What will happen when you compile and run the following code?

public static void main(String[] args){
		int i = 0;
		for(i = 100; i >= 0; i -= 10 ){
			System.out.print(i + ", ");
		}	
	}
A.100, 90, 80, 70, 60, 50, 40, 20, 10, 0,
B.100, 90, 80, 70, 60, 50, 40, 20, 10,
C.90, 80, 70, 60, 50, 40, 20, 10, 0,
D.None of the above


9. What will happen when you compile and run the following code?

public static void main(String[] args){
		int i = 0;
		for(i = 0; i < 3 ; i++){
			continue;
		}
		System.out.println(i);
	}
A.0
B.2
C.3
D.Compilation error


10. What will happen when you compile and run the following code?

public static void main(String[] args){
		for(int i = 0; i < 10; i++){
			if(i % 2 == 0)
				continue;
			System.out.println(i);
		}
	}
A.Code will print all even numbers between 0 to 10
B.Code will print all odd numbers between 0 to 10
C.Code will not compile
D.none


11. What will happen when you compile and run the following code?

public static void main(String[] args){
		for(int i = 0; i < 5; i++){
			System.out.print( (char)('a' + i) );
		}
	}
public static void main(String[] args){
		for(int i = 0; i < 5; i++){
			System.out.print( (char)('a' + i) );
		}
	}
A.01234
B.No output
C.abcde
D.Compilation error


12. What is the output of the following program?

public static void main(String[] args){
		for(char c = 'a' ; c < 'd'; c++){
			System.out.print(c);
		}
	}
A.Code will not compile
B.Code will print abc
C.Code will print 012
D.None of the above


13. An enhanced FOR loop work with only Collection type data. Examples of Collection are ___.

A.Array Class type or any regular array variable
B.ArrayList
C.HashMap, HashSet
D.ALL


14. What is the output of the Java program with the Enhanced FOR loop below?

public static void main(String[] args){
		String countries[] = {"Orange", "Apple", "Grapes"};
		int i=0;
		for(String str: countries)
		{
		  if(i<2)
		    ;
		  else
		    break;
		  System.out.print(str + ",");
		  i++;
		}
		}
A.Orange, Apple, Grapes
B.Orange, Apple,
C.Orange,
D.none


15. A BREAK or CONTINUE statement applies only to the ___ loop.

A.Inner loop or the loop containing break or continue
B.always Outer loop
C.Sometimes inner loop, sometimes outer loop
D.none