Skip to main content

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 the following code?

public class Outer 

{

   private int x = 0;              //Line 1

   private static int y = 0;     //Line 2

   public class Nested 

    {

        public static void test()     //Line 3

{

    System.out.println(x + "" +y);   //Line 4

}

    }

}


A. Line 1 & 2 B. Line 3 & 4

C. Line 3 only D. Line 2, 3 & 4



Ans: C



3. What will be the Output of the following Code snippet?


LocalDate ld = LocalDate.parse("2021-Month.MARCH-20"); 

System.out.println(ld);


A. 2021-03-20

B. 2021-MAR-20

C. Compilation Error

D. Runtime Error



Ans: D




4. What will be the Output of the following Code snippet?


String s = new String("--this-is-new-island--");     

String [] tokens = s.split("-", 4);   

System.out.println(tokens[2].length()); 


A. 2        B. 4       C. 3       D. None of These




Ans: B



5. What will be the Output of the following Code snippet?


String str = new String("Bollywood"); 

String sub = str.substring(1, 8);        

sub = sub.replace('o', 'e');                     

System.out.println(sub);  


A. ellywe B. ellywee

C. ellywo D. ollywo



Ans: B



6. What will be the Output of the following Code snippet?


StringBuilder sb = new StringBuilder(0);   //Line 1 

sb.append("Hello");

sb.ensureCapacity(8);

System.out.println(sb.capacity());


A. Compilation Error in Line 1

B. 8

C. 12

D. 5




Ans: C 



7. What will be the Output of the following Code snippet?


StringBuilder sb = new StringBuilder(6); 

sb.ensureCapacity(8);

sb.setLength(2); //Line1

System.out.println(sb.capacity());



A. Compilation Error in Line 1

B. 8

C. 12

D. 14



Ans: D



8. What will be the Output of the following Code snippet?


StringBuilder sb = new StringBuilder("BOBY"); 

StringBuilder rev = sb.reverse();         //Line 1

System.out.println(sb.equals(rev));     //Line 2


A. true

B. false

C. Compilation Error in Line 1

D. Compilation Error in Line 2




Ans: A



9. What will be the Output of the following Code snippet?


StringBuilder sb = new StringBuilder("BOB"); 

StringBuilder rev = new StringBuilder(sb);   //Line 1       

rev.reverse();     

System.out.println(sb.equals(rev));               //Line 2


A. true

B. false

C. Compilation Error in Line 1

D. Compilation Error in Line 2




Ans: B




10. What will be the output of the following code snippet?


String s = new String("this is an island");               

System.out.println(s.lastIndexOf("is") + s.lastIndexOf("is", 11));


A. 115 B. 1111      C. 16 D. 22




Ans: D




11. What will be the output of the following code snippet?


String s = new String("--this-is-an-island--");     

String [] tokens1 = s.split("-", -1);      

String [] tokens2 = s.split("-", 0);   

System.out.println(tokens1.length - tokens2.length); 


A. 0             B. 1         C. 2         D. 3



Ans: C




12. What will be the output of the following code snippet?

LocalTime lt = LocalTime.of(17, 35, 25, 50000);

System.out.println(lt);


A. 17:35:25.000050

B. 17:35:25.500000

C. 17:35:25.500

D. None of These



Ans: A




13. What will be the Output of the following Code snippet?


LocalDate ld = LocalDate.of(1984, 8, 31); 

System.out.println(ld);


A. 1984-08-31

B. 1984-8-31

C. java.time.DateTimeParseException

D. java.time.DateTimeException




Ans: A



14. What will be the Output of the following Code snippet?


LocalDate ld = LocalDate.of(1988, 2, 29); 

System.out.println(ld);


A. 1988-02-29

B. 1988-2-29

C. java.time.DateTimeParseException

D. java.time.DateTimeException




Ans: A




15. What will be the output of the following code snippet?


String s = new String("--this-is-an-island--");     

String [] tokens = s.split("-", -1);     

System.out.println(tokens.length); 


A. 5             B. 6         C. 7             D. 8


Ans: D




16. What will be the output of the following code snippet?


String s = new String("this is an island");               

System.out.println(s.lastIndexOf("is") +""+ s.lastIndexOf("is", 10));


A. 115 B. 1111      C. 16 D. 22


Ans: A



17. What will be the Output of the following code snippet?


String s1 = new String("Canada"); 

String s2 = new String("Canada East");                           

System.out.println(s2.compareTo(s1)); 


A. -5          B. 5          C. 32         D. None of These


Ans: B



18. 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


19. Which of the following is a valid Abstract class?


A. abstract class A { abstract void test() {} }

B. abstract final class A { abstract void test(); }

C. abstract class A { final int x = 10; }

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


Ans: C



20. Which of the following is a valid Functional Interface?


i. interface A { abstract void test() {} }

ii. interface A { abstract boolean equals(Object ob); }

iii. interface A { void test();

                         boolean equals(Object ob);  }

iv. interface A { abstract demo(); }


A. i & iv           B. only iii         C. iii & iv          D. ii & iv




Ans: B



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 must also be declared abstract.

B. Abstract classes can have constructors.

C. A class without any abstract method can be declared abstract class.

D. None of These



Ans: D



22. Which of the following is FALSE about final keyword in Java?


A. It is used to prevent the inheritance of a class.

B. It is used to declare the constant in Java.

C. Local variables can not be declared final.

D. Abstract class can not be declared final.




Ans: C



23. Which of the following keyword is NOT allowed with Constructor?

i. final ii. static iii. private


A. ii & iii B. i & ii

C. Only ii D. i, ii & iii


Ans: B



24. What will be the output of the following Program?

   class A

{

   void test(){System.out.println("In A");}

   class B extends A

{

   public void test(){System.out.println("In B");}

}    

   class Test

{

   public static void main(String [] arg)

     {

A a1 = new B();          //Line 1

a1.test();

     }

}


A. In A B. Compilation Error in Line 1

C. In B D. Runtime Error


Ans: C



25. What will be the output of the following Program?

   class A

{

   static void test(){System.out.print("In A");}

   class B extends A

{

   public static void test(){System.out.print("In B");}

}    

   class Test

{

   public static void main(String [] arg)

     {

A a1 = new B();    //Line 1

a1.test();

     }

}


A. In A B. Compilation Error in Line 1

C. In B D. Runtime Error



Ans: A



26. What will be the output of the following Program?

class A{

  A(int x)

{System.out.print("Hi ");}

       }

class B extends A{

  B(int x)

{System.out.print("Hello");}

  public static void main(String [] r)

    {

B b1 = new B(1);

    }

}


A. Hello B. Hi Hello

C. Hi D. Compilation Error


Ans: D





27. 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


28. Which of the following is FALSE about the Singleton Class?

A. It provides the private constructor only.

B. It provides a static factory method to return the instance of the class.

C. It provides a public static reference of the same class type.

D. None of These




Ans: C




29. We can use these features of an abstract class even without inheriting it.

A. all the public static and non static members

B. all the public static members

C. all the public non static members

D. all the constructors




Ans: B



30. Which of the following statements is correct?


public interface Guard{

            void doYourJob();

}

abstract public class Dog implements Guard{ }


A. This code will compile without any errors.

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 not compile, because method doYourJob() in interface Guard must be                     defined abstract.


 

Ans: A



31. 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



32. 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


33. 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's variables and methods must be static.

C. It does not have direct access to non-static members of the enclosing class.

D. It must extend the enclosing class.


Ans: C



34. 

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



35. 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 



36. Which of the following exception can be thrown from the following statement?

LocalTime lt = LocalTime.now();

A. DateTimeException

B. DateTimeParseException

C. NullPointerException

D. None of These



Ans: D




37. Which of the following is a possible output of the following code snippet?

LocalTime t = LocalTime.now();

A. 9:18:36.123

B. 23:00:00.123

C. 11:10:12:123

D. 11:09:60.123


Ans: B



38. What will be the output of the following code snippet?


LocalDate d = LocalDate.of(2021, Month.July, 28);

System.out.println(d);


A. 2021-JULY-28

B. 2021-07-28

C. 2021-July-28

D. Compilation Error



Ans: D




39. Given: a class X inherits two functional interfaces A and B with following methods respectively:

void test();

int test(int a);


Which of the following statement is TRUE?

A. class X will have to override both the methods as

                 public void test() {}

public int test(int x) {return 1;}   

B. class X can not inherit both A and B simultaneously

C. class X can be compiled if and only if it is declared abstract

D. class X will have to override at least one of the two methods

     to remain non-abstract.


Ans: A



40.

Given: a class X inherits two interfaces A and B:

interface A {static void test() {} }

interface B{ default int test(int a){return 0;} }


Which of the following statement is TRUE?

A. class X will have to override both the methods as

                 public static void test() {}

public int test(int a) {return 0;}   

B. class X can not inherit both A and B simultaneously

C. class X can be compiled without overriding any method

D. class X will have to override only one method i.e.

     public int test(int a) { return 0; }

     

Ans: C


         

41. 

Given: a class X inherits two interfaces A and B:

interface A {default void test() {} }

interface B{ default int test(int a){return 0;} }


Which of the following statement is TRUE?

A. class X will have to override both the methods as

                 public void test() {}

public int test(int a) {return 0;}   

B. class X can not inherit both A and B simultaneously

C. class X can be compiled without overriding any method

D. class X will have to override only one method i.e.

     public int test(int a) { return 0; }


Ans: C



42. 

Given: a class X inherits two interfaces A and B:

interface A {default void test() {} }

interface B{ default int test(){return 0;} }


Which of the following statement is TRUE?

A. class X will have to override both the methods as

                 public void test() {}

public int test() {return 0;}   

B. class X can not inherit both A and B simultaneously

C. class X can be compiled without overriding any method

D. class X will have to override only one method i.e.

     public int test(int a) { return 0; }


Ans: B



43. 


Given: a class X inherits two interfaces A and B:

interface A{ static void test() {} }

interface B{ int test(); }


Which of the following statement is TRUE?

A. class X will have to override both the methods as

                 public static void test() {}

public static int test() {return 0;}   

B. class X can not inherit both A and B simultaneously

C. class X can be compiled without overriding any method

D. class X will have to override only one method i.e.

     public int test() { return 0; }


Ans: D



44.

Which of the following member(s) of Enclosing class can not be

accessed by Non-static nested class?

(i) private members (ii) static members     (iii) Constructor


A. i & ii

B. i & iii

C. ii & iii

D. None of These


Ans: D



45. 

Which of the following member(s) of Enclosing class can not be

accessed by static nested class?

(i) instance variables (ii) private static members    (iii) Constructor


A. only i 

B. i & iii

C. ii & iii

D. i, ii & iii


Ans: A



46.

Which of the following is FALSE about a local class defined inside a method?


A. It can be declared final.

B. It can be declared abstract.

C. It can be not be declared public.

D. It can not inherit any other class.



Ans: D



47.

Which of the following statements, if placed in a class other than Outer or Nested,                            instantiates an instance of the nested class?


public class Outer 

{

    public class Nested 

    {

        public void test() { }

    }

}


A. Outer.Nested obj = new Outer.Nested();

B. Outer.Nested obj = new Nested();

C. Nested obj = new Outer.Nested();

D. None of These


Ans: D 



48.

Which of the following is FALSE about anonymous class?

A. It can not define any constructor.

B. It can not declare any static method.

C. It can not declare any final data member.

D. Both A & B


Ans: A



Comments

  1. The content of this blog is essential to learning stage people. Thanks for sharing.
    RPA course in Chennai
    RPA Online Course
    RPA Course In Bangalore

    ReplyDelete
  2. Great Post. Very informative. Keep Sharing!!

    Join Online Java Classes

    For more details about the course fee, duration, classes, certification, and placement call our expert at 70-70-90-50-90

    ReplyDelete
  3. Great Code Post. Very informative & Helpfully. Keep Sharing!!
    Class 7 Curriculum

    ReplyDelete

Post a Comment

Popular posts from this blog

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

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 MyE