Ticker

6/recent/ticker-posts

Ad Code

Java actice MCQs Codetantra.

Java Programming Practice MCQs Codetantra.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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(); }

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

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

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

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

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

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

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

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

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

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.

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.

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

34. Which of the following statement is correct?

interface My { double sum(int x, double b); }

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

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

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

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.

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; }

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; }

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; }

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; }

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

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

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.

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





Post a Comment

0 Comments