EmSAT Achieve Exam  >  EmSAT Achieve Tests  >  Mock Tests for EmSAT Achieve 2025  >  EmSAT Java Mock Test - EmSAT Achieve MCQ

EmSAT Java Mock Test - EmSAT Achieve MCQ


Test Description

30 Questions MCQ Test Mock Tests for EmSAT Achieve 2025 - EmSAT Java Mock Test

EmSAT Java Mock Test for EmSAT Achieve 2025 is part of Mock Tests for EmSAT Achieve 2025 preparation. The EmSAT Java Mock Test questions and answers have been prepared according to the EmSAT Achieve exam syllabus.The EmSAT Java Mock Test MCQs are made for EmSAT Achieve 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for EmSAT Java Mock Test below.
Solutions of EmSAT Java Mock Test questions in English are available as part of our Mock Tests for EmSAT Achieve 2025 for EmSAT Achieve & EmSAT Java Mock Test solutions in Hindi for Mock Tests for EmSAT Achieve 2025 course. Download more important topics, notes, lectures and mock test series for EmSAT Achieve Exam by signing up for free. Attempt EmSAT Java Mock Test | 100 questions in 100 minutes | Mock test for EmSAT Achieve preparation | Free important questions MCQ to study Mock Tests for EmSAT Achieve 2025 for EmSAT Achieve Exam | Download free PDF with solutions
EmSAT Java Mock Test - Question 1

Predict the output of following program. Note that fun() is public in base and private in derived.

class Base {
    public void foo() { System.out.println("Base"); }
}
 
class Derived extends Base {
    private void foo() { System.out.println("Derived"); } 
}
 
public class Main {
    public static void main(String args[]) {
        Base b = new Derived();
        b.foo();
    }

Detailed Solution for EmSAT Java Mock Test - Question 1

It is compiler error to give more restrictive access to a derived class function which overrides a base class function.

EmSAT Java Mock Test - Question 2

Predict the output of following Java Program

// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
 
class Parent extends Grandparent {
    public void Print() {
        System.out.println("Parent's Print()");
    }
}
 
class Child extends Parent {
    public void Print() {
        super.super.Print(); 
        System.out.println("Child's Print()");
    }
}
 
public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 2

In Java, it is not allowed to do super.super. We can only access Grandparent's members using Parent. For example, the following program works fine.

// Guess the output
// filename Main.java
class Grandparent {
    public void Print() {
        System.out.println("Grandparent's Print()");
    }
}
 
class Parent extends Grandparent {
    public void Print() {
        super.Print();  
        System.out.println("Parent's Print()");
    }
}
 
class Child extends Parent {
    public void Print() {
        super.Print();  
        System.out.println("Child's Print()");
    }
}
 
class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.Print();
    }
}

EmSAT Java Mock Test - Question 3

Which of the following is FALSE about arrays in Java?

Detailed Solution for EmSAT Java Mock Test - Question 3

In Java, arrays are objects, they have members like length. The length member is final and cannot be changed. All objects are allocated on heap in Java, so arrays are also allocated on heap.

EmSAT Java Mock Test - Question 4

Predict the output?
// file name: Main.java
public class Main {
    public static void main(String args[]) {
       int arr[] = {10, 20, 30, 40, 50};
       for(int i=0; i < arr.length; i++)
       {
             System.out.print(" " + arr[i]);              
       }
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 4

The program initializes an integer array arr[] with five elements. The for loop iterates through each element of the array using arr.length as the condition to print all the elements. The output will be the values 10, 20, 30, 40, 50, printed with a space between them.

EmSAT Java Mock Test - Question 5

Which of the following is a valid Java data type for storing a true/false value?

Detailed Solution for EmSAT Java Mock Test - Question 5

The boolean data type in Java is used to store a true or false value. int is used for integers, float is for decimal values, and String is for storing sequences of characters.

EmSAT Java Mock Test - Question 6

Predict the output of the following program.

 class Test
{
    public static void main(String[] args)
    {
        Double object = new Double("2.4");
        int a = object.intValue();
        byte b = object.byteValue();
        float d = object.floatValue();
        double c = object.doubleValue();

        System.out.println(a + b + c + d );

    }
}

Detailed Solution for EmSAT Java Mock Test - Question 6

The object.intValue() and object.byteValue() return 2. object.doubleValue() and object.floatValue() return 2.4. Summing them gives 2 + 2 + 2.4 + 2.4 = 8.8.

EmSAT Java Mock Test - Question 7

Predict the output of following Java program.

class Test { 
    public static void main(String[] args) { 
      for(int i = 0; 0; i++) 
      { 
          System.out.println("Hello"); 
          break; 
      } 
    } 

Detailed Solution for EmSAT Java Mock Test - Question 7

In the for loop, the condition is 0, which is invalid in Java. The condition of a for loop must evaluate to a boolean expression, not an integer. The code will result in a compile-time error.

EmSAT Java Mock Test - Question 8

class Main {   
   public static void main(String args[]) {      
         int t;      
         System.out.println(t); 
    }   
}

Detailed Solution for EmSAT Java Mock Test - Question 8

Unlike class members, local variables of methods must be assigned a value to before they are accessed, or it is a compile error.

EmSAT Java Mock Test - Question 9

Predict the output of following Java program

class T {
  int t = 20;
  T() {
    t = 40;
  }
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}

Detailed Solution for EmSAT Java Mock Test - Question 9

The values assigned inside constructor overwrite the values initialized with declaration.

EmSAT Java Mock Test - Question 10

Predict the output?

package main;
class T {
  int t = 20;
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}

Detailed Solution for EmSAT Java Mock Test - Question 10

The T class has an instance variable t initialized to 20. When an object t1 is created, it accesses the t variable and prints its value, which is 20.

EmSAT Java Mock Test - Question 11

Output of following Java program

class Point {
  int m_x, m_y;
  
  public Point(int x, int y) { m_x = x; m_y = y; }
  public Point() { this(10, 10); }
  public int getX() { return m_x; }
  public int getY() { return m_y; }
  
  public static void main(String args[]) {
    Point p = new Point();
    System.out.println(p.getX());
  }

Detailed Solution for EmSAT Java Mock Test - Question 11

The constructor Point() calls the parameterized constructor this(10, 10) and initializes m_x and m_y with values 10, so getX() returns 10.

EmSAT Java Mock Test - Question 12

What is the output of the following Java program?

final class Complex {
    private double re, im;
    public Complex(double re, double im) {
        this.re = re;
        this.im = im;
    }
    Complex(Complex c) {
        System.out.println("Copy constructor called");
        re = c.re;
        im = c.im;
    }            
    public String toString() {
        return "(" + re + " + " + im + "i)";
    }            
}
class Main {
    public static void main(String[] args) {
        Complex c1 = new Complex(10, 15);
        Complex c2 = new Complex(c1);    
        Complex c3 = c1;  
        System.out.println(c2);
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 12

The copy constructor is called when c2 is created from c1, initializing c2 with the values of c1.

EmSAT Java Mock Test - Question 13

What is the output of the following Java program?

class Test
{
    static int a;

    static
    {
        a = 4;
        System.out.println("inside static block");
        System.out.println("a = " + a);
    }

    Test()
    {
        System.out.println("inside constructor");
        a = 10;
    }

    public static void func()
    {
        a = a + 1;
        System.out.println("a = " + a);
    }

    public static void main(String[] args)
    {
        Test obj = new Test();
        obj.func();
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 13

The static block initializes a to 4, the constructor sets it to 10, and func() increments it to 11.

EmSAT Java Mock Test - Question 14

What is the output of the below Java Code?

class Test extends Exception { }
 
class Main {
   public static void main(String args[]) { 
      try {
         throw new Test();
      }
      catch(Test t) {
         System.out.println("Got the Test Exception");
      }
      finally {
         System.out.println("Inside finally block ");
      }
  }
}

Detailed Solution for EmSAT Java Mock Test - Question 14

In Java, the finally block is always executed after the try-catch block, regardless of whether an exception is thrown or not. It is typically used for cleanup operations.

EmSAT Java Mock Test - Question 15

Predict the output of following Java program

class Main {
   public static void main(String args[]) {
      try {
         throw 10;
      }
      catch(int e) {
         System.out.println("Got the  Exception " + e);
      }
  }
}

Detailed Solution for EmSAT Java Mock Test - Question 15

In Java only throwable objects (Throwable objects are instances of any subclass of the Throwable class) can be thrown as exception. So basic data type can no be thrown at all. Following are errors in the above program
Main.java:4: error: incompatible types
         throw 10;
               ^
  required: Throwable
  found:    int
Main.java:6: error: unexpected type
      catch(int e) {
            ^
  required: class
  found:    int
2 errors

EmSAT Java Mock Test - Question 16

What is the output of the below Java Code?

class Base extends Exception {}
class Derived extends Base  {}

public class Main {
  public static void main(String args[]) {
   // some other stuff
   try {
       // Some monitored code
       throw new Derived();
    }
    catch(Base b)     { 
       System.out.println("Caught base class exception"); 
    }
    catch(Derived d)  { 
       System.out.println("Caught derived class exception"); 
    }
  }

Detailed Solution for EmSAT Java Mock Test - Question 16

In Java, exceptions are caught in the order they are declared in the catch blocks. Since Base is a superclass of Derived, the catch(Base b) block will always catch instances of Derived as well, making the catch(Derived d) block unreachable. This results in a compilation error because the catch(Derived d) block is redundant and will never be executed.

EmSAT Java Mock Test - Question 17

Predict the output of the following program.

class Test
{   int count = 0;

    void A() throws Exception
    {
        try
        {
            count++;
            
            try
            {
                count++;

                try
                {
                    count++;
                    throw new Exception();

                }
                
                catch(Exception ex)
                {
                    count++;
                    throw new Exception();
                }
            }
            
            catch(Exception ex)
            {
                count++;
            }
        }
        
        catch(Exception ex)
        {
            count++;
        }

    }

    void display()
    {
        System.out.println(count);
    }

    public static void main(String[] args) throws Exception
    {
        Test obj = new Test();
        obj.A();
        obj.display();
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 17

The throw keyword is used to explicitly throw an exception. In the third try block, an exception is thrown, which goes to the first catch block. Inside that block, the exception is re-thrown, but control then moves to the second catch block.

EmSAT Java Mock Test - Question 18

Which of these is a super class of all errors and exceptions in the Java language?

Detailed Solution for EmSAT Java Mock Test - Question 18

All the errors and exception types are subclasses of the built in class Throwable in the java language. Throwable class is the superclass of all errors and exceptions in the Java language. Option (B) is correct.

EmSAT Java Mock Test - Question 19

The built-in base class in Java, which is used to handle all exceptions is

Detailed Solution for EmSAT Java Mock Test - Question 19

The Throwable class is the built-in base class for both errors and exceptions in Java. It is the parent class for Exception and Error. While Exception handles exceptions that can be caught and handled by the program, Error represents serious issues (like JVM errors) that are generally not handled by the program. So the correct answer is "Throwable".

EmSAT Java Mock Test - Question 20

Predict the output of the following program.

class Test
{
    String str = "a";

    void A()
    {
        try
        {
            str +="b";
            B();
        }
        catch (Exception e)
        {
            str += "c";
        }
    }

    void B() throws Exception
    {
        try
        {
            str += "d";
            C();
        }
        catch(Exception e)
        {
            throw new Exception();
        }
        finally
        {
            str += "e";
        }

        str += "f";

    }
    
    void C() throws Exception
    {
        throw new Exception();
    }

    void display()
    {
        System.out.println(str);
    }

    public static void main(String[] args)
    {
        Test object = new Test();
        object.A();
        object.display();
    }

}

Detailed Solution for EmSAT Java Mock Test - Question 20

'throw' keyword is used to explicitly throw an exception. finally block is always executed even when an exception occurs. Call to method C() throws an exception. Thus, control goes in catch block of method B() which again throws an exception. So, control goes in catch block of method A().

EmSAT Java Mock Test - Question 21

What is the output of the below Java Code?

class Test
{
    public static void main (String[] args)
    {
        try
        {
            int a = 0;
            System.out.println ("a = " + a);
            int b = 20 / a;
            System.out.println ("b = " + b);
        }

        catch(ArithmeticException e)
        {
            System.out.println ("Divide by zero error");
        }

        finally
        {
            System.out.println ("inside the finally block");
        }
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 21

On division of 20 by 0, divide by zero exception occurs and control goes inside the catch block. Also, the finally block is always executed whether an exception occurs or not.

EmSAT Java Mock Test - Question 22

What is the output of the below Java Code?

class Test
{
    public static void main(String[] args)
    {
        try
        {
            int a[]= {1, 2, 3, 4};
            for (int i = 1; i <= 4; i++)
            {
                System.out.println ("a[" + i + "]=" + a[i] + "\\n");
            }
        }
        
        catch (Exception e)
        {
            System.out.println ("error = " + e);
        }
        
        catch (ArrayIndexOutOfBoundsException e)
        {
            System.out.println ("ArrayIndexOutOfBoundsException");
        }
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 22

ArrayIndexOutOfBoundsException has been already caught by base class Exception. When a subclass exception is mentioned after base class exception, then error occurs.

EmSAT Java Mock Test - Question 23

public class Myfile 

    public static void main (String[] args) 
    {
        String biz = args[1]; 
        String baz = args[2]; 
        String rip = args[3]; 
        System.out.println("Arg is " + rip); 
    } 
}

Select how you would start the program to cause it to print: Arg is 2

Detailed Solution for EmSAT Java Mock Test - Question 23

Arguments start at array element 0 so the fourth arguement must be 2 to produce the correct output.

EmSAT Java Mock Test - Question 24

Which of the following are valid calls to Math.max?
1. Math.max(1,4)
2. Math.max(2.3, 5)
3. Math.max(1, 3, 5, 7)
4. Math.max(-1.5, -2.8f)

Detailed Solution for EmSAT Java Mock Test - Question 24

(1), (2), and (4) are correct. The max() method is overloaded to take two arguments of type int, long, float, or double.
(3) is incorrect because the max() method only takes two arguments.

EmSAT Java Mock Test - Question 25

Which of the following would compile without error?

Detailed Solution for EmSAT Java Mock Test - Question 25

The return value of the Math.abs() method is always the same as the type of the parameter passed into that method.
In the case of A, an integer is passed in and so the result is also an integer which is fine for assignment to "int a".
The values used in B, C & D respectively are a double, a float and a long. The compiler will complain about a possible loss of precision if we try to assign the results to an "int".

EmSAT Java Mock Test - Question 26

What is the value of "d" after this line of code has been executed?
double d = Math.round ( 2.5 + Math.random() );

Detailed Solution for EmSAT Java Mock Test - Question 26

The Math.random() method returns a number greater than or equal to 0 and less than 1 . Since we can then be sure that the sum of that number and 2.5 will be greater than or equal to 2.5 and less than 3.5, we can be sure that Math.round() will round that number to 3. So Option B is the answer.

EmSAT Java Mock Test - Question 27

public class Test2 
{
    public static int x;
    public static int foo(int y) 
    {
        return y * 2;
    }
    public static void main(String [] args) 
    {
        int z = 5;
        assert z > 0; /* Line 11 */
        assert z > 2: foo(z); /* Line 12 */
        if ( z < 7 )
            assert z > 4; /* Line 14 */

        switch (z) 
        {
            case 4: System.out.println("4 ");
            case 5: System.out.println("5 ");
            default: assert z < 10;
        }

        if ( z < 10 )
            assert z > 4: z++; /* Line 22 */
        System.out.println(z);
    }
}

which line is an example of an inappropriate use of assertions?

Detailed Solution for EmSAT Java Mock Test - Question 27

Assert statements should not cause side effects. Line 22 changes the value of z if the assert statement is false.
Option A is fine; a second expression in an assert statement is not required.
Option B is fine because it is perfectly acceptable to call a method with the second expression of an assert statement.
Option C is fine because it is proper to call an assert statement conditionally.

EmSAT Java Mock Test - Question 28

What will be the output of the program (when you run with the -ea option) ?
public class Test 
{  
    public static void main(String[] args) 
    {
        int x = 0;  
        assert (x > 0) : "assertion failed"; /* Line 6 */
        System.out.println("finished"); 
    } 
}

Detailed Solution for EmSAT Java Mock Test - Question 28

An assertion Error is thrown as normal giving the output "assertion failed". The word "finished" is not printed (ensure you run with the -ea option)
Assertion failures are generally labeled in the stack trace with the file and line number from which they were thrown, and also in this case with the error's detail message "assertion failed". The detail message is supplied by the assert statement in line 6.

EmSAT Java Mock Test - Question 29

What will be the output of the program?
public class Test 
{
    public static int y;
    public static void foo(int x) 
    {
        System.out.print("foo ");
        y = x;
    }
    public static int bar(int z) 
    {
        System.out.print("bar ");
        return y = z;
    }
    public static void main(String [] args ) 
    {
        int t = 0;
        assert t > 0 : bar(7);
        assert t > 1 : foo(8); /* Line 18 */
        System.out.println("done ");
    }
}

Detailed Solution for EmSAT Java Mock Test - Question 29

The foo() method returns void. It is a perfectly acceptable method, but because it returns void it cannot be used in an assert statement, so line 18 will not compile.

EmSAT Java Mock Test - Question 30

public class Test 

    public void foo() 
    {
        assert false; /* Line 5 */
        assert false; /* Line 6 */
    } 
    public void bar()
    {
        while(true)
        {
            assert false; /* Line 12 */
        } 
        assert false;  /* Line 14 */
    } 
}

What causes compilation to fail?

Detailed Solution for EmSAT Java Mock Test - Question 30

Option D is correct. Compilation fails because of an unreachable statement at line 14. It is a compile-time error if a statement cannot be executed because it is unreachable. The question is now, why is line 20 unreachable? If it is because of the assert then surely line 6 would also be unreachable. The answer must be something other than assert.
Examine the following:
A while statement can complete normally if and only if at least one of the following is true:
- The while statement is reachable and the condition expression is not a constant expression with value true.
-There is a reachable break statement that exits the while statement.
The while statement at line 11 is infinite and there is no break statement therefore line 14 is unreachable. You can test this with the following code:

public class Test80 

    public void foo() 
    {
        assert false; 
        assert false; 
    } 
    public void bar()
    {
        while(true)
        {
            assert false; 
            break; 
        } 
        assert false;  
    } 
}

View more questions
27 tests
Information about EmSAT Java Mock Test Page
In this test you can find the Exam questions for EmSAT Java Mock Test solved & explained in the simplest way possible. Besides giving Questions and answers for EmSAT Java Mock Test, EduRev gives you an ample number of Online tests for practice
Download as PDF