-->

Object oriented programming Java -4 || diplomachakhazana

null

diplomachakhazana



 JOIN US

Sr.no Select Any Option
1 Object Oriented Programming Java MCQ Page 1
2 Object Oriented Programming Java MCQ Page 2
3 Object Oriented Programming Java MCQ Page 3
4 Object Oriented Programming Java MCQ Page 4

Object oriented programming Java

1. Which among the following is called first, automatically, whenever an object is created?
a) Class
b) Constructor
c) New
d) Trigger

Answer: b


2. Which among the following is not a necessary condition for constructors?
a) Its name must be same as that of class
b) It must not have any return type
c) It must contain a definition body
d) It can contains arguments

Answer: c


3. Which among the following is correct?
a) class student{ public: int student(){} };
b) class student{ public: void student (){} };
c) class student{ public: student{}{} };
d) class student{ public: student(){} };

Answer: d


4. In which access should a constructor be defined, so that object of the class can be created in any function?
a) Public
b) Protected
c) Private
d) Any access specifier will work

Answer: a


5. How many types of constructors are available for use in general (with respect to parameters)?
a) 2
b) 3
c) 4
d) 5

Answer: a


6. If a programmer defines a class and defines a default value parameterized constructor inside it.
He has not defined any default constructor. And then he try to create the object without passing arguments, which among the following will be correct?
a) It will not create the object (as parameterized constructor is used)
b) It will create the object (as the default arguments are passed)
c) It will not create the object (as the default constructor is not defined)
d) It will create the object (as at least some constructor is defined)

Answer: b


7. Which of the following statements are incorrect?
a) default constructor is called at the time of object declaration
b) constructor can be parameterized
c) finalize() method is called when a object goes out of scope and is no longer needed
d) finalize() method must be declared protected

Answer: c


8. What will be the output of the following Java code?


  1.     class box 

  2.      {

  3.         int width;

  4.         int height;

  5.         int length;

  6.         int volume;

  7.         void finalize() 

  8.         {

  9.             volume = width*height*length;

  10.             System.out.println(volume);

  11.         }

  12.         protected void volume() 

  13.        {

  14.             volume = width*height*length;

  15.             System.out.println(volume);

  16.        } 

  17.     }    

  18.     class Output 

  19.     { 

  20.         public static void main(String args[])

  21.         {

  22.             box obj = new box();

  23.             obj.width=5;

  24.             obj.height=5;

  25.             obj.length=6;

  26.             obj.volume();

  27.         } 

  28.     }


a) 150
b) 200
c) Run time error
d) Compilation error

Answer: a


9. What will be the output of the following Java code?


  1.      class box 

  2.     {

  3.         int width;

  4.         int height;

  5.         int length;

  6.         int volume;

  7.         box() 

  8.         {

  9.             width = 5;

  10.             height = 5;

  11.             length = 6;

  12.         }

  13.         void volume() 

  14.         {

  15.              volume = width*height*length;

  16.         } 

  17.     }    

  18.     class constructor_output 

  19.     {

  20.         public static void main(String args[])

  21.         {

  22.             box obj = new box();

  23.             obj.volume();

  24.             System.out.println(obj.volume);

  25.         }

  26.    }


a) 100
b) 150
c) 200
d) 250

Answer: b


10. Which operator is used by Java run time implementations to free the memory of an object when it is no longer needed?
a) delete
b) free
c) new
d) none of the mentioned

Answer: d


11. Which function is used to perform some action when the object is to be destroyed?
a) finalize()
b) delete()
c) main()
d) none of the mentioned

Answer: a



12. Default constructor must be defined, if parameterized constructor is defined and the object is to be created without arguments.
a) True
b) False

Answer: a



13. Which object will be created first?

class student 

{  

    int marks;

};

student s1, s2, s3;


a) s1 then s2 then s3
b) s3 then s2 then s1
c) s2 then s3 then s1
d) all are created at same time

Answer: a


14. Which among the following is correct for the class defined below?




class student

{

    int marks;

    student(){}

    student(int x)

    { 

         marks=x; 

    }

}

class abc

{

public static void main(String args[])

{

    student s1=new student(100);

    student s2=new student();

      

}

}





a) Object s3, syntax error
b) Only object s1 and s2 will be created
c) Program runs and all objects are created
d) Program will give compile time error

Answer: c

15. For constructor overloading, each constructor must differ in ___________ and __________
a) Number of arguments and type of arguments
b) Number of arguments and return type
c) Return type and type of arguments
d) Return type and definition

Answer: a





16. Which among the following is true?
a) Default constructor can’t be defined by the programmer
b) Default parameters constructor isn’t equivalent to the default constructor
c) Default constructor can be called explicitly
d) Default constructor is and always called implicitly only

Answer: c


17. Which type of constructor can’t have a return type?
a) Default
b) Parameterized
c) Copy
d) Constructors don’t have a return type

Answer: d


18. Default constructor initializes all data members as ___________
a) All numeric member with some garbage values and string to random string
b) All numeric member with some garbage values and string to null
c) All numeric member with zero and strings to random value
d) All numeric member with zero and strings to null

Answer: d

19. If constructors of a class are defined in private access, then __________
a) The class can’t be inherited
b) The class can be inherited
c) Instance can be created only in another class
d) Instance can be created anywhere in the program

Answer: a

20. A Java constructor is like a method without ___.

A) statements

B) return type

C) argument list

D) None

Answer: B

21. The name of a constructor and the name of a class are ___.


A) Same

B) Different

Answer: A

22. The placement of a constructor inside a class should be ___.

A) Always at the beginning of class

B) Always at the end of class

C) Anywhere in the class

D) None

Answer: C


23. The purpose of a Java constructor is ___.

A) Initialization of variables with passed data

B) Writing custom code

C) Accepting other objects as inputs

D) All the above

Answer: D

24. Memory is allocated to an object once the execution of ___ is over in Java language.

A) main method

B) constructor

C) destructor

D) None

Answer: B

25. What is the output of the below Java program?

public class TestingConstructor

{

  void TestingConstructor()

  {

    System.out.println("Amsterdam");    

  }

 

  TestingConstructor()

  {

    System.out.println("Antarctica");   

  }

        

  public static void main(String[] args)

  {

    TestingConstructor tc = new TestingConstructor();

  }

}


A) Antarctica

B) Amsterdam

C) No output

D) Compiler error

Answer: A

26. In Java, a constructor with no parameters or no arguments is called ___ constructor.

A) Default constructor

B) User-defined constructor

Answer: A

27.In Java, a constructor with one or more arguments or parameters is called a ___ constructor.

A) Default constructor

B) User-defined constructor or Non-default constructor

Answer: B

28. The compiler adds a default no-argument constructor to a class if it ___.

A) does not define a constructor at all.

B) defines at least one constructor with arguments

Answer: A

29. Overloading of constructors in Java means adding more than ___ constructors with the different argument list.

A) 1

B) 2

C) 3

D) 8

Answer: A

30. What is the output of the below Java program with constructors?

public class Constructor2

{

  int count=10;

  Constructor2(int count)

  {

    System.out.println("Count=" + count);

  }

 

  public static void main(String[] args)

  {

    Constructor2 con = new Constructor2();

  }

}

A) Count=0

B) Count=10

C) Compiler error

D) None of the above

Answer: C

31. What is the output of the below Java program with overloaded constructors?

public class Constructor3

{

  int birds=10;

  Constructor3()

  {

    this(20);

  }

  Constructor3(int birds)

  {

    System.out.println("Birds=" + birds);

  }

  public static void main(String[] args)

  {

    Constructor3 con = new Constructor3();

  }

}

A) Birds=0

B) Birds=10

C) Birds=20

D) Compiler error

Answer: C

32. In Java, you can pass __ variables from one constructor to another overloaded constructor.

A) local variables

B) static variables

C) non-static variables

D) local and static variables

Answer: D

33. Choose the correct way of calling the second constructor from the first constructor in the below code options.

A) 

Constructor5()

{

  int a=30;

  this('A');

}

Constructor5(char c)

{

 //

}

B) 

Constructor5()

{

  int a=30;

  this('A');

  System.out.println("Success");

}

Constructor5(char c)

{

  //

}

C) 

Constructor5()

{

  this('A');

  System.out.println("Success");

}

Constructor5(char c)

{

  //

}

D) All the above

Answer: C

34. What is the output of the below Java program with many constructors?








public class Constructor7

{

  Constructor7(int a)

  {

    System.out.println("Book=" + a);

  }

  Constructor7(float a)

  {

    System.out.println("Pen="+ a );

  }

  public static void main(String[] args)

  {

    Constructor7 con = new Constructor7(50.5f);

  }

}

A) Book=50

B) Pen=50.5

C) Compiler error

D) None of the above

Answer: B

35. What is the output of the below Java program with many constructors?
public class Constructor8

{

  Constructor8(boolean a)

  {

    System.out.println("MODEM="+ a );

  }

  Constructor8(float a)

  {

    System.out.println("ROUTER=" + a);

  }

  public static void main(String[] args)

  {

    Constructor8 con1 = new Constructor8(50);

    Constructor8 con2 = new Constructor8(false);

  }

}

A) 

ROUTER=50.0

MODEM=false

B) 

ROUTER=50

MODEM=false

C) Compiler error

D) None

Answer: A

36. What is the output of the below Java program with overloaded constructors?










public class Jiraffe

{

  Jiraffe(int sugarcanes)

  {

    System.out.println("Eats "+ sugarcanes + " Sugarcanes");

  }

  Jiraffe(int age, int...sugarcanes)

  {

    System.out.println("Eats "+ sugarcanes[0] + " Sugarcanes");

  }

  public static void main(String[] args)

  {

    Jiraffe jiff2 = new Jiraffe(40);

    Jiraffe jiff = new Jiraffe(5,10);

  }

}

A) 

2.Eats 40 Sugarcanes

2.Eats 10 Sugarcanes

B) 

1.Eats 40 Sugarcanes

2.Eats 10 Sugarcanes

C) Compiler error

D) None

Answer: B

37. Choosing a suitable overloaded constructor happens at ___ time in Java.

A) Compile-time

B) Run time

Answer: B

38. Java constructor overloading follows ___ principle in Object-Oriented programming.

A) Inheritance

B) Encapsulation

C) Polymorphism

D) None

Answer: C

39. Java allows calling or invoking a method from a constructor. State TRUE or FALSE.

A) TRUE

B) FALSE

Answer: A

40. What is the output of the below Java program?

public class Constructor9

{

  Constructor9()

  {

    show();     

  }

  void show()

  {

    System.out.println("JAM JAM");

  }

  public static void main(String[] args)

  {

    Constructor9 con = new Constructor9();

  }

}

A) JAM JAM

B) No output

C) Compiler error

D) None

Answer: A

41. 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);

   }

}






A) 20

B) 0

C)Compiler Error

Answer: A

42. 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);

   }

}







A) 20

B) 40

C)Compiler Error

Answer: B

43. Which of the following is/are true about constructors in Java?

1) Constructor name should be same as class name.

2) If you don't define a constructor for a class, 

    a default parameterless constructor is automatically

    created by the compiler. 

3) The default constructor calls super() and initializes all 

   instance variables to default value like 0, null.

4) If we want to parent class constructor, it must be called in 

   first line of constructor.

A) 1

B) 1 and 2

C)1,2 and 3

D)1,2,3, and 4

Answer:D

44. Is there any compiler error in the below Java program?


class Point {

    int m_x, m_y; 

    public Point(int x, int y) {    m_x = x;    m_y = y;  }

    public static void main(String args[]) 

    {

      Point p = new Point();

    }

}






A) Yes

B) NO

Answer: A

45. 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());

  }



A) 10

B) 0

C)Compiler Error

Answer:A


46. What is the output of the program?


class Test

{

    static int a;

     

    static

    {

        a = 4;

        System.out.println ("inside static blockn");

        System.out.println ("a = " + a);

    }

     

    Test()

    {

        System.out.println ("ninside constructorn");

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

 

    }

}


A)inside static block

a = 4

inside constructor

a = 11


B)Compiler Error

c)Run Time Error

D)inside static block

a = 4

inside constructor

a = 5

Answer: A







diplomachakhazana


0 Comments