Computer Knowledge

Object-Oriented Programming

2,686 Questions

Object-oriented programming questions test core computer science concepts like classes, inheritance, polymorphism, and encapsulation. The focus includes Java program structures, method overloading, and memory allocation for objects. This topic is essential for technical sections in various recruitment tests.

Java class definitionsMethod overriding rulesPolymorphism conceptsGeneric type parametersMemory allocation in objects

Object-Oriented Programming Questions

Multiple choice technology programming languages
  1. Java Foundation Course

  2. Java Fundamental Classes

  3. Java Foundation Classes

  4. None of the above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

JFC stands for Java Foundation Classes, which is the GUI framework for Java including Swing.

Multiple choice technology programming languages
  1. True

  2. False

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The statement 'Foo.Print()' is incorrect because Print() is an instance method (not static). Instance methods must be called on an object instance, not the class name. The correct syntax would be 'f.Print();' where 'f' is the instance. Class-name syntax (Foo.Print()) is only valid for static methods.

Multiple choice technology programming languages
  1. Member Function

  2. Same name as Class name

  3. Default constructor initializes all non-initialized fields and variables to zero.

  4. Compiler adds an empty constructor, if we do not write our own constructor

Reveal answer Fill a bubble to check yourself
A,B,C,D Correct answer
Explanation

Constructors ARE member functions (A), MUST have the same name as the class (B), the default constructor initializes numeric fields to zero (C), and the compiler DOES provide an empty default constructor if you don't write one (D). All four statements are correct.

Multiple choice technology programming languages
  1. Java Foundation Course

  2. Java Fundamental Classes

  3. Java Foundation Classes

  4. None of the above

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

JFC stands for Java Foundation Classes. This is the correct and official name for the collection of graphical user interface (GUI) components in Java, which includes Swing and other GUI APIs. 'Course' and 'Fundamental' are not the correct expansion.

Multiple choice technology programming languages
  1. foreach(x) System.out.println(z);

  2. for(int z : x) System.out.println(z);

  3. while( x.hasNext()) System.out.println( x.next());

  4. for( int i=0; i< x.length; i++ ) System.out.println(x[i]);

Reveal answer Fill a bubble to check yourself
B,D Correct answer
Explanation

The parameter int... x is treated as an array of integers (int[]). The enhanced for-loop for(int z : x) and the standard index-based loop for(int i=0; i&lt; x.length; i++) are both valid Java syntax for iterating over an array.

Multiple choice technology programming languages
  1. Alpha a = x;

  2. Foo f= (Alpha)x;

  3. Foo f= (Delta)x;

  4. Beta b = (Beta)(Alpha)x;

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

Attempting to cast a Beta object to Delta will cause ClassCastException because the actual object is of type Beta, not Delta. Options A and B are safe casts (upcast or interface reference). Option D is a convoluted but valid cast chain.

Multiple choice technology programming languages
  1. The class is fully encapsulated

  2. The ownerName variable breaks encapsulation

  3. The cardlD and limit variables break polymorphism

  4. The setCardlnformation method breaks encapsulation

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

To solve this question, the user needs to understand the concepts of encapsulation and polymorphism.

Encapsulation is the concept of hiding the implementation details of a class and making the variables and methods private to ensure that they cannot be accessed from outside the class.

Polymorphism, on the other hand, is the ability of an object to take on many forms, depending on the context in which it is used.

Now let's examine each option and determine which is true:

A. The class is fully encapsulated: This option is incorrect. While the class does have private variables (cardlD and limit), the ownerName variable is public, which means it can be accessed from outside the class. Therefore, the class is not fully encapsulated.

B. The ownerName variable breaks encapsulation: This option is correct. The ownerName variable is declared as public, which means it can be accessed and modified from outside the class. This breaks the concept of encapsulation because the implementation details of the class are not fully hidden.

C. The cardlD and limit variables break polymorphism: This option is incorrect. The cardlD and limit variables have nothing to do with polymorphism. They are private variables that can only be accessed from within the class.

D. The setCardlnformation method breaks encapsulation: This option is incorrect. The setCardlnformation method is a public method, which is used to set the values of the private variables. It does not break encapsulation because it is providing a controlled way of accessing the private variables.

Therefore, the correct answer is:

The Answer is: B

Multiple choice technology programming languages
  1. Euro returns correct values

  2. An exception is thrown at runtime.

  3. Yen and Euro both return correct values

  4. Compilation fails because of an error at line 25.

Reveal answer Fill a bubble to check yourself
A,D Correct answer
Explanation

Line 12 lacks a return type, but assuming that is a typo, line 25 fails compilation because country is private in Money and cannot be accessed directly via super.country. Euro compiles and returns correct values because it uses the public getter super.getCountry().

Multiple choice technology programming languages
  1. import com.sun.scjp.Geodetics;public class TerraCarta {public double halfway(){ return Geodetics.DIAMETER/2.0; } }

  2. import static com.sun.scjp.Geodetics;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }

  3. import static com.sun.scjp.Geodetics. *;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }

  4. package com.sun.scjp;public class TerraCarta {public double halfway() { return DIAMETER/2.0; } }

Reveal answer Fill a bubble to check yourself
A,C Correct answer
Explanation

Option A uses a regular import and accesses the static field using the class name (Geodetics.DIAMETER), which is valid. Option C uses a static import with wildcard (import static com.sun.scjp.Geodetics.*) which allows accessing DIAMETER directly without the class prefix. Option B is incorrect because static import requires either a wildcard or a specific static member name. Option D is incorrect because DIAMETER is not accessible without class qualification in a different package.

Multiple choice technology programming languages
  1. Direction d = NORTH;

  2. Nav.Direction d = NORTH;

  3. Direction d = Direction.NORTH;

  4. Nav.Direction d = Nav.Direction.NORTH;

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

The Direction enum is defined inside the Nav class, so it's a nested enum. To access its constants, you must qualify it with the outer class name first: Nav.Direction. Then to access the NORTH constant, you use Nav.Direction.NORTH. Options A and B fail because Direction alone is not in scope. Option C is incorrect because Direction.NORTH tries to use Direction as a standalone type, but it's nested inside Nav.

Multiple choice technology programming languages
  1. Foo { public int bar() { return 1; } }

  2. new Foo { public int bar() { return 1; } }

  3. newFoo() { public int bar(){return 1; } }

  4. new class Foo { public int bar() { return 1; } }

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To instantiate an anonymous inner class implementing the Foo interface, you must use the new keyword followed by the interface name and parentheses, and then provide the implementation: new Foo() { public int bar() { return 1; } }.

Multiple choice technology programming languages
  1. import sun.scjp.Color.*;

  2. import sun.scjp.Color; import static sun.scjp.Color.*;

  3. import sun.scjp.Color; import static sun.scjp.Color.GREEN;

  4. import static sun.scjp.Color.*;

Reveal answer Fill a bubble to check yourself
B,C Correct answer
Explanation

To use the enum constant GREEN directly without prefixing it with Color., you must statically import it. import static sun.scjp.Color.*; imports all constants, while import static sun.scjp.Color.GREEN; imports only GREEN. Both require importing the Color class or its static members.

Multiple choice technology programming languages
  1. public interface B extends A { }

  2. public interface B implements A {}

  3. public interface B instanceOf A {}

  4. public interface B inheritsFrom A { }

Reveal answer Fill a bubble to check yourself
A Correct answer
Explanation

To solve this question, the user needs to know the basics of Java interfaces and inheritance.

Option A is the correct answer: A programmer can create a child interface by using the "extends" keyword and specifying the parent interface name. In this case, interface B can be created by extending interface A as follows:

public interface B extends A {}

Option B is incorrect because the "implements" keyword is used to implement an interface, not to extend it.

Option C is incorrect because "instanceOf" is a keyword used to test if an object is an instance of a class, not to create an interface that inherits from another interface.

Option D is incorrect because "inheritsFrom" is not a valid keyword in Java to indicate inheritance.

Therefore, the answer is: A. public interface B extends A {}