Java - Online Test - Oops - javasearch.buggybread.com


Share

Search Java Test Questions


 223 test questions in repository.




Java - Test Questions on 'oops' - 50 questions found


Which access specifiers can be used with top level class ?Core Java
public or default
public or private
public or protected
protected or default

  access specifier  oops  java  class

Which of the following can be marked static ?Core Java
Methods , Variables and Initialization Blocks.
Methods , Variables , Initialization Blocks and Outer Classes and nested Classes.
Methods , Variables , Initialization Blocks and Outer Classes.
Methods , Variables , Initialization Blocks and nested Classes.

  oops  java  static  nested classes  static nested classes

Which of the following cannot be marked static ?Core Java
Constructors , Classes ( Outer ) , Classes ( nested ), Interfaces , Local variables , Inner Class methods and instance variables.
Constructors , Classes ( Outer ) , Interfaces , Local variables , Class variables , Class Methods , Inner Class methods and instance variables.
Constructors , Classes ( Outer ) , Interfaces , Local variables , Inner Class methods and instance variables.
Constructors , Classes ( Outer ) , Classes (Nested), Interfaces , Local variables , Inner Class methods and instance variables.

  oops  java  static  nested classes  static nested classes

Interface can only have ...Core Java
Member elements and Methods.
Static Variables and Static Methods.
Static Final Variables and Instance Method Declarations.
Member Elements , Instance Methods, Static variables and Static Methods.

  java  oops  interfaces

Which of the following is not the difference between Singleton and Static class ( Class with static members only ) ?Core Java
Only one object can be created for Singleton class whereas No objects are created for static class.
Singleton class instance is initiated using new keyword whereas static class instance is created using static method.
Singleton class can be serialized whereas Static class cannot be.
Singleton Class can participate in runtime Polymorphism whereas Static class cannot.

  java  oops  singleton  design pattern  static class

Which of the following is false about Constructors ?
Constructor can be overloaded
A no argument constructor is provided by the compiler if we declare only constructors with arguments.
Constructors shouldn't have any return types , not even void.
If super is not explicitly called, still super() is intrinsically added by the compiler.

  java  oops  constructor

What is an instance variable?Core Java
An instance or object of an class
The field of an object
Any variable of a class
None of the above

  java  oops  oop  instance variable  variables

Which of the following do you think is the primary reason you would never use a static class even the application doesn't need multiple requests or threads ?Core Java
Serialization
Runtime Polymorphism
Lazy Loading
Memory

  static class  static vs singleton  java  oops  objects

Which of the following Java feature promotes access protection or Hiding ?Core Java
Inheritance
Encapsulation
Abstraction
Composition

  java  java concepts  java features  oops concepts  oops features  access protection  information hiding

What is the relationship between Vehicle and Engine in this example ?

public class Vehicle {
   Enginer engine;
   public void move(){
      engine = new Engine();
      engine.start();
   }
}
Core Java
Composition ( Vehicle has a Engine )
Composition ( Engine has a Vehicle )
Inheritance ( Vehicle is a Engine )
Inheritance ( Engine is a Vehicle )

  composition  inheritance  oops concepts

What is the relationship between Car and Vehicle in the following code ?

public class Car extends Vehicle{
   Engine engine;   
   
   public static void main(String[] args){
      Vehicle vehicle = new Car();
      car.move();
   }   

   public void move(){
      engine = new Engine();
      engine.start();
   }
}
Core Java
Composition ( Vehicle has a Car )
Composition ( Car has a Vehicle )
Inheritance ( Vehicle is a Car )
Inheritance ( Car is a Vehicle )

  Inheritance  Composition  Oops concepts

What is the problem with the following code ?

public class Car extends Vehicle{
   Vehicle vehicle;
   
   Car(){
      super();
      this.vehicle = new Vehicle();
   }
}
Core Java
There is an Inheritance as well as Composition relationship between Vehicle and Car which is not permitted
We cannot initialize the parent class instance within the constructor
Call to super is illegal
There is no problem

  Composition  Inheritance  OOPS concepts

Which of the following Java feature specify hash-a relationship between objects ?Core Java
Inheritance
Composition
Polyorphism
Encapsulation

  oops

Which of the following is not type of inner classes ?Core Java
Simple Inner Class
Static Nested Inner Class
Method Nested Static Inner Class
Anonymous Inner Class

  java  classes  inner classes  nested classes

Which of the following is true ?Core Java
We can serialize static variables
We can serialize transient variables
We can serialize final variables
We can serialize instance methods

  serialization  java  file io

Overridden methods must have the same ... Core Java
name
name and argument list
name, argument list, and return type
name, argument list, return type and belong to the same class

  java  overriding

How can we execute a Java class independently if it doesn't have a static main method ?Core Java
By initiating the flow in any of the static method
By initiating the flow in any of static block
By initiating the flow in any other instance method named as main
By initiating the flow in any other instance method named as main and making it final

  main method  java

Which of the following class creates mutable objects ?Core Java
Boolean
File
String
StringBuffer

  java  immutable

Which of the following is wrong for final instance variables ?Core Java
They cannot be changed after initialization
They can be initialized directly within static method
They can be declared and initialized together at the same place
They can be initialized within constructor

  java  final  final variable  java keywords

Which of the following is false for final ?Core Java
Final methods cannot be overriden
Final methods cannot be overloaded
Final classes cannot be subclassed
Final class cannot be abstract

  java  final  java keyword

When String literals are compared using ==, they always returns true if the string values are same because .. Core Java
of overridden compareTo method
of overridden compare method
of String Pool
== means that the object contents are equal

  

Which of the following Java feature promotes Code Re usability ?Core Java
Abstraction
Encapsulation
Inheritance
Polymorphism

  java  java concepts  java features  code reusability

Which of the following is true ?Core Java
Composition is Tightly Bound
Inheritance is Tightly Bound
Object can only hold reference of only one other object
A Class cannot be extended by multiple classes

  

In case of String Literals ..Core Java
x==y on all literals always returns false
x.equals(y) on all literals always returns false
if x.equals(y) returns true, x==y returns true too
if x.equals(y) returns false, x==y returns true

  

What is a default constructor ?Core Java
Constructor without parameters declared by user
Constructor provided by Java if no constructor is declared
Constructor with empty body
All of the above

  Constructor

How can we create objects if we make the constructor private ?Core Java
We can't create objects if constructor is private
We can only create objects if we follow singleton pattern
We can only create one object
We can create new object through static method or static block

  constructor   private constructor

What will be the output of following code ?

String str1 = "String1";
String str2 = "String2";
str1.concat("String3");
System.out.print(str1);
System.out.print(str2);
Core Java
String1String2
String1String3String3
String1String3String1String3
String1String1

  

Which of the following can throw ClassCastException ?Core Java
UpCasting
DownCasting
Casting to incompatible data type
Casting to Strings

  casting

Which of the following can be overridden ?Core Java
final instance methods
final static methods
non final instance methods
non final static methods

  overriding

x instanceOf y returns false ..Core Java
if x is an instance of y class
if x is an instance of class implementing y interface
if x is an instance of class extending y class
if x is an instance of Class which is a parent of Y class

  instaceOf

if classes B and C extends Class A, Which of the following initialization is correct ?Core Java
B b = new C();
C c = new B();
B b = new A();
A a = new B();

  inheritance

What will be the output of following code ?

public class BuggyBread {
   
   private int x;
   private int y;
   
   BuggyBread(int x,int y){};
   
   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread();
      System.out.println(buggybread.x);
   }
}
Core Java
0
null
compilation error due to uninitialized element
compilation error due to constructor

  default constructor

What will be the output upon executing following class ?

public class BuggyBread {

static {
System.out.println("Static Block");
}

{
System.out.println("Instance Initialization Block");
}

BuggyBread(){
System.out.println("Constructor");
}

public static void main(String[] args){
System.out.println("Main Method");
new BuggyBread();
}
}
Core Java
Instance Initialization Block
Constructor
Static Block
Main Method
Static Block
Instance Initialization Block
Constructor
Main Method
Main Method
Static Block
Instance Initialization Block
Constructor
Static Block
Main Method
Instance Initialization Block
Constructor

  static block  Instance Initialization Block

What will be the output of following code ?

public class BuggyBread {

   private int x;
   private Integer y;

   private BuggyBread(int x,int y){};

   public static void main(String[] args){
      BuggyBread buggybread = new BuggyBread(1,2);
      System.out.println(buggybread.x);
   }
}
Core Java
compilation error due to private constructor
compilation error due to uninitialized elements
0 null
0 0

  constructor

Which of the following class creates immutable objects ?Core Java
String
StringBuffer
StringBuilder
None of these create immutable objects.

  String  StringBuffer  StringBuilder  Immutable

Which of the following methods are used by Java Garbage Collection Mechanism ?Core Java
final
finally
finalize
All of the above

  garbage collection  finalize

What is the problem with this code ?

public class Car extends Vehicle{   
   int x;
   
   Car(int y){
      x = 5;
   }
   
   Car(){
      this(5);
      super();
   }
}
Core Java
We cannot overload constructors
Constructors should have type
we should have called super() before this(5)
We cannot have both super() and this() in a constructor

  Constructor  this  super

What is the problem with this code ?

public class Car extends Vehicle{
   int x;

   Car(int y){
      x = 5;
   }

   Car(){
      super();
      this.x = 5;
   }
}
Core Java
We cannot overload constructors
We cannot call super in overloaded constructor
we cannot have this and super in constructor
There is no problem

  constructor  this  super

The following code is an example of

public class Car extends Vehicle{
   int x;

   Car(int y){
      x = 5;
   }

   Car(){
      this(5);
   }
}
Core Java
Constructor Overloading
Constructor Chaining
Both Constructor Overloading and Chaining
None of above

  constructor  constructor overloading  constructor chaining

What is the problem with following code ?

public class Car extends Vehicle{
   int x;

   void Car(int y){
      x = 5;
   }

   void Car(){
      this(5);
   }
}
Core Java
We cannot use this() within normal method
We cannot chain methods
We cannot overload constructors
We cannot use return type void with overloaded methods

  constructor

The use of volatile keyword facilitates ..Core Java
Making Use of Cache for better Performance
Avoiding use of Cache
Making use of Backward as well as Forward Cache
Keeping only one copy of variable in Cache

  volatile  java keywords

Which of the following is a Marker Interface ?Core Java
Runnable
Serializable
Cloneable
Both Serializable and Cloneable

  marker interface  interfaces

Static Polymorphic in Java is achieved through .. Core Java
Method Overloading
Method Overriding
Variable Overloading
Variable Overriding

  polymorphism  static polymorphism

Runtime Polymorphism in Java is achieved through ..Core Java
Method Overloading
Method Overriding
Variable Overloading
Variable Overriding

  runtime polymorphism  polymorphism

Which of following are serialized ?Core Java
static variables
transient variables
instance variables
method local variables

  serialization

In which case finally won't get executed ?Core Java
in case of exception
in case of normal execution
in case of return statement before end of try block
in case of force program termination

  finally

Which of following can be nested into another ?Core Java
class within another class
class within interface
interface within class
All of above

  nested classes   nested interface  inner classes

Which of following is not the method of object class ?Core Java
run
notify
wait
clone

  object class

Which memory segment holds String Pool ?Core Java
Stack
Heap
Code Segment
Class Segment

  memory management  string pool

Default Constructor is provided by Java ... Core Java
To Reserve Memory
To provide at least one instance method
To Make it look good
To initialize the object state

  Constructor







comments powered by Disqus