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


Java Quiz-4(if-else)

Java Multiple Choice Questions(if-else)


1. An IF or ELSE IF statement accepts ___ as input before branching.

A.boolean
B.int
C.float
D.char


2. An ELSE statement must be preceded by ___ statement in Java.

A.IF
B.ELSE IF
C.IF or ELSE IF
D.NONE


3. The condition of an IF statement evaluates to boolean only if the expression contains?

A.logical operators
B.relational operators
C.boolean operands
D.ALL


4. An IF-ELSE statement is better than a SWITCH statement in which scenario below?

A.Checking for More-than condition
B.Checking for Less-than condition
C.Checking for Ranges
D.ALL


5. What is the output of Java program with IF statement?

if(1)
{
  System.out.println("OK");
}
A.OK
B.No output
C.Compiler error
D.NONE


6. What is the output of the Java program?

int a=10;
if(a==9)
  System.out.println("OK ");
  System.out.println("MASTER");					
else
  System.out.println("BYE");
A.OK MASTER
B.BYE
C.Compiler error
D.NONE


7. What is the output of the Java program?

String name1="FOX", name2="DOG";
if(name1 == "FOX")
  System.out.print("FOX ");
  System.out.println("GOOD");					
if(name2 == "CAT")
  System.out.println("DINO");
A.FOX DINO
B. FOX GOOD DINO
C.Compiler error
D.NONE


8. What is the output of the Java program?

String name="dino";
if(name == "dino")
	System.out.print("DINO");
System.out.println("GOOD");
A. DINO GOOD
B.DINO
C.GOOD
D.Compiler error


9. What is is the output of the Java program?

int marks=85;
if(marks >= 80)
  System.out.println("DISTINCTION");
else if(marks >=35)
  System.out.println("PASS");
A.DISTINCTION
B.PASS
C.Compiler error
D.NONE


10. Which of the following is not a decision-making statement?

A.if-else
B.if-else if
C.switch
D.for


11. Which of the following is not a branching statement.

A.if-else
B.break
C.continue
D.return


12. Which is not a looping statement?

A.for
B.while
C.if-else if-else
D.do-while


13.  Which of these selection statements test only for equality?

A.if
B.switch
C.if & switch
D.for


14. Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?

A.for
B.if
C.switch
D.do-while


15. What will be the output of the following Java program?

 public static void main(String args[])
     {
         int a = 5; 
         int b= 6;
         if ((a= 1) == b)
             System.out.print(a);
         else 
             System.out.print(++a);
     }
A.5
B.4
C.7
D.2


Tuesday, July 13, 2021

Java Quiz-3

Java Multiple Choice Questions


1. Which of these can not be used for a variable name in Java?

A.identifier
B.keyword
C.identifier and keyword both
D.none


2. Abstraction in Java can be achieved by using ________________

A.Abstract classes
B.interfaces
C.Abstract classes and interfaces both
D.packages and abstract classes


3. ______________ class members are the members of the class which do not belong to any instance of that class.

A.public
B.private
C.static
D.final


4. Static variable of a class can be accessed using _____________________ syntax.

A.ObjectName.staticVarName
B.ClassName.staticVarName
C.ObjectName.ClassName
D.staticVarName.className


5. public static final declared variables are ____________

A.global variables
B.global constants
C.not accessible
D.not possible


6. Which statement is true about static members of a class?

A.Static methods can directly access static variables of the class and manipulate them.
B.Static members can not access non static members(instance variables or instance methods)
C.Static methods can’t use this or super references.
D.All of the above


7. What is the output of the following program?

public class UI {
	static int i = 1;
	public static void main(String args[])
	{
	System.out.println(i+" , ");
	m(i);
	System.out.println(i);
	}
	public static void m(int i)
	{
	i += 2;
	}
}
A.1,3
B.3,1
C.1,1
D.1,0


8. What is the output of the following program?

public class UI {
	static int i = 1;
	public static void main(String args[])
	{
	System.out.println(i+" , ");
	m(i);
	System.out.println(i);
	}
	public static void m(int i)
	{
	i += 2;
	}
}
A.1,3
B.3,1
C.1,1
D.Error


9. Which statement is false

A.Static members of a class are shared among all instances of that class
B.Each instance of the class have its own static members
C.A non static method can be called inside a static method
D.A class can have static variables and static methods


10. What will be the output of the program?

public class UI {

    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A"); 
        }  
        catch (Exception ex) 
        {
            System.out.print("B");  
        } 
        finally 
        {
            System.out.print("C"); 
        } 
        System.out.print("D"); 
    }  
    public static void badMethod() 
    {
        throw new Error(); /* Line 22 */
    }

}
A.ABCD
B.Compilation Failure
C.C is printed before exiting with an error message.
D.BC is printed before exiting with an error message.


11. What will be the output of the program?

public class UI {

    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print("A");  
        } 
        catch (RuntimeException ex) /* Line 10 */
        { 
            System.out.print("B"); 
        } 
        catch (Exception ex1) 
        { 
            System.out.print("C"); 
        } 
        finally 
        {
            System.out.print("D"); 
        } 
        System.out.print("E"); 
    } 
    public static void badMethod() 
    { 
        throw new RuntimeException(); 
    } 
}
A.BD
B.BCD
C.BDE
D.BCDE


Java Quiz-2

Java Multiple Choice Questions


1. Java is developed by ____________________.

A.Sun Microsystems
B.Adobe
C.Microsoft
D.Facebook


2. ___________ is one of the java features that enables java program to run anywhere anytime.

A.Object-Oriented
B.Multithreading
C.Platform-Independent
D.Case Sensitive


3. Java compiler translates source code into _____________

A. Bytecode (Virtual Machine Code)
B.Bitcode
C.Machine level code
D.none


4. input .java file is compiled into ____________ file as output by java compiler.

A..exe
B..jar
C..class
D..out


5. The name of the Java Compiler is______________.

A.Turbo Java
B.javac
C.java
D.jvm


6. What is a feature of Java Programming Language?

A.Security and Case-Sensitive
B.Architectural neutral and Platform independent
C.Multithreading and Automatic Garbage Collection
D.all of the above


7. Java is a _____________ programming language.

A.interpreted
B.compiled
C.interpreted and compiled both
D.translated


8. What is true about Class in java?

A.A class has attributes and methods
B.A Class can be considered as factory for producing the objects having same attributes and methods with different states
C.A Class can be declared with public or default modifier
D.All of the above


9. Which of the following is not assignment operator?

A.+=
B.==
C.%=
D.=


10. __________ variables and methods can be called without using the objects

A.static
B.final
C.abstract
D.public


11. Which of the following statements is true?

A.Java supports operator overloading
B.Java supports interfaces
C.Java supports pointers
D.Java supports multiple inheritance


12. __________ is a special method in java that enables an object to initialize itself when it is created

A.new()
B.this()
C.constructor
D.super()


13. ______ is an implicit reference created by the runtime itself referring to the current object itself.

A.super
B.this
C.that
D.new


14. We cannot create a subclass of _________ class.

A.abstract
B.static
C.final
D.private


15. ________ allows to dynamically allocate memory for the object on heap space and create object reference on the heap stack.

A.this keyword
B.new keyword
C.constructor
D.main() method


Java Quiz-1

Java Multiple Choice Questions

1. Which of the following is not a primitive data type?

A.byte
B.Integer
C.double
D.long


2. In Java, Which of the following does not have a superclass?

A.System
B.String
C.Object
D.Date


3. All elements of an Array have same ____________?

A.index
B.value
C.data type and scope
D.none


4. A variable becomes a Constant if it has a ______________ modifier.

A.static
B.abstract
C.final
D.const


5. Which of the following can be used to copy data from one array to another?

A.System.clone
B.System.arraycopy
C.java.lang.clone
D.none


6. Which of the following option leads to the portability and security of Java?

A.Bytecode is executed by JVM
B.The applet makes the Java code secure and portable
C.Use of exception handling
D.Dynamic binding between objects


7. Which of the following is not a Java features?

A.Dynamic
B.Architecture Neutral
C.Use of pointers
D.Object-oriented


8. _____ is used to find and fix bugs in the Java programs.

A.JVM
B.JRE
C.JDK
D.JDB


9. What is the return type of the hashCode() method in the Object class?

A.Object
B.int
C.long
D.void


10. Which of the following is a valid long literal?

A.ABH8097
B.L990023
C.904423
D.0xnf029L


11. What does the expression float a = 40 / 0 return?

A.NaN
B.0
C.Infinity
D.Arithmetic Exception


12. Evaluate the following Java expression, if x=3, y=5, and z=10:


++z + y - y + z + x++

A.24
B.23
C.20
D.25


13. Which of the following tool is used to generate API documentation in HTML format from doc comments in source code?

A.javap tool
B.javaw command
C.Javadoc tool
D.javah command


14. Which option is false about the final keyword?

A.A final method cannot be overridden in its subclasses.
B.A variable declared as final becomes a constant
C.A final class cannot be extended
D.A final method can be overrriden


15. Which of the following is a reserved keyword in Java?

A.object
B.extends
C.strictfp
D.system