Skip to main content

Mock Test - 1



1. Which of the following is a byte stream class?

A. ObjectInputStream
B. Writer
C. PrintWriter
D. Scanner

Ans: A

2. Which exception is thrown by read() method in InputStream class?

A. IOException
B. InterruptedException
C. FileNotFoundException
D. None of These


Ans: A


3. Which of the following class is subclass of FilterInputStream?

A. InputStream
B. ObjectOutputStream
C. FileInputStream
D. BufferedInputStream 

Ans: D


4. Which of these class contains the print() and println() methods?

A. System
B. out
C. PrintStream
D. BUfferedOutputStream

Ans: C

5. Which of these is a method to clear all the data present in output buffer without terminating the stream?

A. clear()
B. fflush()
C. flush()
D. close()


Ans: C

6. Which of these classes defined in java.io and used for file-handling are abstract?
(i) InputStream        (ii) PrintStream   
(iii) Reader        (iv) FileInputStream

A. Only i     B. i and ii    C. Only iii    D. i and iii

Ans: D

7. What is the output of this program?

class MyException extends Exception
{
   MyException()
     {
    super("Exception Occured");
     }
   public String toString()
     {
    return "Problem";
     }
}

class Output
{
   public static void main(String args[])
     {
    try {
        throw new MyException();
        }
        catch(MyException e)
        {
        System.out.print(e);
        }
     }
}

A. Exception Occured
B. MyException: Exception Occured
C. Problem
D. None of These


Ans: C


8. Identify the incorrect statement among the following.

A. throw is used to explicitly throw a user defined exception.
B. throws is used to specify that the method can throw exception
c. Throwable is an abstract class which is parent of Exception.
D. None of the above

Ans: C

9. What will be the Output of the following Program?
 public class Test
 {
   public static void main(String[] args)
     {
    int[] array = new int[2];
    for (int i=0; i<2; i++)
      {
       try{
        array[i] = i;
        System.out.println(i);
          }
       catch(NullPointerException e)
          {
        System.out.println("NullPointer Exception");
          }
       finally
          {
        System.out.println("Finally Executed");
        array = null;
          }
      }
     }
}

A) 0
   Finally Executed
   NullPointer Exception
   Finally Executed

B) 0
   Finally Executed
   1
   Finally Executed   

C) 0
   Finally Executed

D) None of These


Ans: A

10. What will be the Output of the following Program?

class exception_handling
   {
     public static void main(String args[])
    {
       try  {   
          throw new Error();
        }
       catch(Throwable e)
        {
          System.out.print("Throwable");
        }
       catch(Exception e)
        {
          System.out.print("Exception");
        }
    }
  }

A. Throwable
B. Exception
C. Compilation Error
D. None of These

Ans: C

11. What happens in a method if an exception is thrown in a try{} and there is NO MATCHING catch block?
A. Program will not Compile.
B. The method passes the exception to its caller.
C. The program ignores that exception.
D. The program halts immediately.


Answer:B

12. Which statement is FALSE about catch block?

A. There must be only one catch block in a try/catch structure.
B. The catch block for a child exception class must FOLLOW that of a parent exception class.
C. If there is no catch block, there must be a finally{}.
D. Both A & B

Ans: D



13. When a method can throw an exception then it is specified by _____ keyword.
A. throws
B. throw
C. finally
D. catch

Ans: A

14. The Exception class is defined in ____ package
A. java.exception
B. java.io
C. java.lang
D. java.util


Ans: C

15. Which exception is thrown When an array element is accessed beyond the array size.

A. ArrayElementsOutOfLimitException
B. ArrayIndexOutOfBoundsException
C. ArrayOutOfBoundsException
D. ArrayElementOutOfBoundsException

Ans: B


16. What should be the minimum changes in code given below to correct it?

public class Car    //Line 1
{
   public abstract void addFuel(); //Line2
}

A. Line 1 should be written as abstract public class Car
B. Line 2 should be written as public abstract void addFuel(){}
C. Line 2 should be written as public void addFuel();
D. Both A & C

Ans: A
--------------------

17. What should be the minimum changes in code given below to correct it?

public class Car    //Line 1
{
   public abstract void addFuel(); //Line2
}

A. Line 1 should be written as abstract public class Car
B. Line 2 should be written as public void addFuel(){}
C. Line 2 should be written as public void addFuel();
D. Either A or B

Ans: D

-------------------

18. Which of the following is a valid abstract class?

A. class A { abstract void test() {} }
B. class A { abstract void test(); }
C. abstract class A { abstract void test(); }
D. public class abstract A { abstract void test(); }

Ans: C

---------------------
19. Which of the following is a valid Functional Interface?

A. interface A { abstract void test() {} }
B. interface A { abstract boolean equals(Object ob); }
C. interface A { abstract void test();
         public boolean equals(Object ob);  }
D. interface A { abstract test(); }

Ans: C

-----------------------

20. Which of the following declares an abstract method in an abstract Java class?

A. public abstract test();
B. private abstract void test();
C. public void abstract test();
D. abstract void test();

Ans: D

21. Which of the following is FALSE about abstract classes in Java?

A. If we inherit an abstract class and do not implement all the abstract methods, then the derived class should also be declared abstract.
B. Abstract classes can have constructors.
C. A class without any abstract method can be declared abstract class.
D. A class can inherit multiple abstract classes.

Ans: D

22. Which of the following is FALSE about interfaces in java.

A. Interfaces can have constructors and abstract methods.
B. An instance of interface can not be created.
C. An interface can contain default and static methods with bodies.
D. An interface can inherit multiple interfaces.

Ans: A

23. Given, abstract class A{}
      class B extends A{
                B(int a){}
               }

Which of the following is INCORRECT?
A. A a = new A();
B. A a = new B(1);
C. B b = new A();
D. Both A & C

ANs: D

24. which of the following statements is correct?

public interface Guard{
        void doYourJob();
}
abstract public class Dog implements Guard{ }

A. This code will not compile, because method doYourJob() in interface Guard must be defined abstract.

B. This code will not compile, because class Dog must implement method doYourJob() from interface Guard.

C. This code will not compile, because in the declaration of class Dog we must use the keyword extends instead of implements.

D. This code will compile without any errors.

Ans: D

25. Which is true about an anonymous inner class?
A. It can extend exactly one class AND implement exactly one interface.
B. It can extend exactly one class OR implement exactly one interface.
C.It can extend exactly one class AND can implement multiple interfaces.
D. It can implement multiple interfaces regardless of whether it also extends a class.

Ans: B

26. Which is true about a local class which is declared inside a method?
A. It can be instantiate anywhere within the same class.
B. It can be declared abstract.
C. It can be declared public.
D. It can be declared static.

Ans: B

27. Which statement is true about a static nested class?
A. You must have a reference to an instance of the enclosing class in order to instantiate it.
B. It does not have direct access to non-static members of the enclosing class.
C. It's variables and methods must be static.
D. It must extend the enclosing class.

Ans: B

28. interface My { double sum(int x, double b); }
Which of the following statement is correct?

A. My x1 = (x, y) -> return x+y;
B. My x1 = (int x, double y) -> x+y;
C. My x1 = (double x, int y) -> return x+y;
D. None of These

Ans: B

29. Which of the following is FALSE about lambda expression?
A. Curly braces in expression body is optional if the body contains a single statement.
B. return keyword is optional if the body has a single expression to return the value.
C. type declaration of all parameters is always compulsory.
D. Both B & C

Ans: C

30. What will be the output of the folowing program?

class Demo
{
   public static void main(String args[])
     {
    try {
            System.out.print("Hello " + 1 / 0);
            }
    catch(Exception e)
        {
               System.out.print("World ");           
        }
     }
}

A. Hello World
B. World Hello
C. World
D. None of These

Ans: C
1 (a) Write a program that reads 10 integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0.       
       
(b) (i) Write a program to sum all the elements of an double type array containing 10 elements.                                  
(ii) Write a program in which define a method to convert an array of double values into array of int values.Invoke that method from the main() method for displaying the output.               

2 (a) Create a class Employee with properties name, salary, department and age. Create method read() in Employee class to read these properties from keyboard and display() method to display these properties. Create another class TestMyObject with main method to invoke read() and display() methods.                                   
           
(b) (i) Java is platform independent, Justify the statement and how the platform independence is achieved?               
(ii) Write a program to demonstrate the use of any 5 methods of String class.                                

3 (a) What do you mean by Nested class in Java? Differeniate between the Inner class and Local class in java with an appropriate example.   

           
(b)(i) Write a program to define two interfaces X and Y each having a default methos and an abstract method. Now define a class Test which inherits both X and Y and override the abstract method of X but not of Y.
Create a class MyClass which inherits Test and in main method create the object of MyClass and invike all the methods.           

(ii) Define Functional Interface and Lambda Expressions in Java with a suitable example.                       

4 (a) Write a program to define a custom exception InvalidStringException such that it displays the message "String is not accepted" when message is printed. Create a class TestString which reads a String at run-time and if the String is having less than 6 characters then throw the InvalidStringException and display the message in catch block.               

           
(b)(i) Differentiate throw and throws exception handling with java code in detail.                                         (ii)  Write a program to create a class named “Vehicle” having attributes name, mileage and price and a parameterized constructor to initialize all the attributes. Vehicle must contain a method public int getPrice() to return the price of the vehicle.
Create 3 Vehicle objects and write them in a file named “record.doc”. Write a method to display the name of the Vehicle with minimum price. (Use ObjectOutputStream)                       


5 (a) Write a program to create two arraylists containing "George","Jim", "John", "Blake", "Kevin", "Michael" and "George","Katie", "Kevin", "Michelle", "Ryan" respectively and find their union, difference, and intersection using appropriate methods of ArrayList class.               
           
(b)(i) What do you mean by Generics? Write a program to create a generic class and instantiate it.                   
(ii) Differentiate between the iterator and list iterator using an appropriate example.                       

Comments

  1. Good Post. I like your blog. Thanks for Sharing
    Java Training in Noida

    ReplyDelete
  2. Thanks for proving this mock test. We will share this with our students for sure. As a JAVA training institute ACL IT academy encourage such articles. Thanks and regards
    ACL IT academy- Java Training Institute in Kolkata

    ReplyDelete
  3. Greetings from California! I'm bored to tears at work so I decided to check out your site on my iphone during lunch break. I really like the knowledge you present here and can't wait to take a look when I get home. I'm shocked at how fast your blog loaded on my mobile .. I'm not even using WIFI, just 3G .. Anyways, amazing site!
    digital marketing Sydney

    ReplyDelete
  4. If you are a beginner in programming world and want to learn programming fast. So I suggest you a a website which have projects with source code and you can use those projects and practice those projects Projects With Source Code

    ReplyDelete
  5. Thank you for sharing Amazing Blog. It's providing very useful guideline for Engineering students.
    get more: Java Programming


    ReplyDelete
  6. APTRON Solutions offers a wide range of JAVA training in Delhi to meet the developing corporate needs. The course materials and syllabus are prepared via trainers who have many years of experience in leading IT companies.
    For More Info: Java Training in Delhi

    ReplyDelete
  7. Really Great work. So here I want to share this amazing typeface with you;

    Royalty Free Fonts

    ReplyDelete
  8. thank you for giving us such a blog

    ReplyDelete
  9. Thank you for giving your precious time for creating such sort of blogs.

    ReplyDelete
  10. Hello.
    Are you looking for an amazing logo design for your business with 50% off I expect You will be impressed with us.Logo Designers

    ReplyDelete
  11. Thank you for sharing useful information with us. please keep sharing like this. And if you are searching a unique and Top University in India, Colleges discovery platform, which connects students or working professionals with Universities/colleges, at the same time offering information about colleges, courses, entrance exam details, admission notifications, scholarships, and all related topics. Please visit below links:

    Manav Rachna International Institute of Research and Studies Faridabad

    NIMS University Jaipur

    Nirma University Ahmedabad

    PES University Bangalore

    Rabindranath Tagore University Bhopal

    ReplyDelete

  12. Nice! This is the great post I like this post and thanks for share to me. eCareerpluz provide IT Skill Programs like Beginner Level Courses, Programming Courses, etc…
    Best Java Training in Madurai | Best C/C++ Training in Madurai | Best Python Training in Madurai | Basic Computer Course | Best Tally Training in Madurai
    https://careerpluz.in/java-training-madurai

    ReplyDelete

Post a Comment

Popular posts from this blog

Practice MCQs

  1. What will be the Output of the following code? class X{                  static int x = 1;                        static class Y {                              static int y = x++;                                static class Z                                                                       {                                                                                         static int z = ++y;                                                                        }                                               }                }   class MainClass{     public static void main(String[] rk)    {                             System.out.print(X.x);                                             System.out.print(X.Y.y);                             System.out.print(X.Y.Z.z);         } } A. 113 B. 123 C. 111 D. None of These Ans: A 2.  Which of the following statement is TRUE about t he following code? public class Outer  {    private i

Practice Set

1. Write a program to create a class Voter which contains attributes name, date_of_birth and voter_id and voter has a Voter_Card. Provide appropriate constructor to initialize all the attributes of the Voter but voter id must be assigned automatically only when the age of the voter is greater than or equal to 18 years. VoterCard is a nested class with attributes voter_id and Voter_name. Make sure that voter card is created only when user is a valid voter and if it is already created then must not be assigned the new voter id. 2. Write a program to define two interfaces UGC and AICTE both having a default method int getAdmission() to take the admission and an abstract method String payFee(). getAdmission() in UGC must ask the percentage in qualifying exam and if the percentage >= 60 then generate the registration number and return. getAdmission() is AICTE must ask the user to join the counseling after 5 days and display the date of counseling and return the counseling token numb