Questions and CORRECT Answers
Define Object-Oriented Programming. - CORRECT ANSWER - the ability to design a
program using classes and objects
What is an Object? - CORRECT ANSWER - an instance of a class. Each object contains
an address and takes up some space in memory.
How do Objects communicate? - CORRECT ANSWER - Objects can communicate
without knowing the details of each other's data or code. The type of message accepted and type
of response returned by the objects are the only necessary elements.
Give an example of an Object? - CORRECT ANSWER - A dog has
states: color, name, breed, etc.
and behaviors: tail wagging, barking, eating, etc.
public class Dog {
String breed;
int age;
String color;
void barking() {
}
void hungry() {
}
void sleeping() {
}
}
,Define a class. - CORRECT ANSWER - A class is a template that is used to create objects
and define object types and methods.
Classes can be defined as the blueprint from which you can create an individual object.
What are the core properties of a class? - CORRECT ANSWER - The data types and
methods that may be used by that object. Classes do not consume any space.
What are advantages of OOPs over Procedure Oriented Programming? - CORRECT
ANSWER - - OOPs makes development and maintenance easier than Procedure-Oriented
languages, especially as the project size increases.
- OOPs provides data hiding (abstraction), but in Procedure-Oriented languages, global data can
be accessed from anywhere.
Define a constructor. - CORRECT ANSWER - A constructor is a block of codes similar to
the method.
Basically, it is a special type of method which is used to initialize the object.
When is a constructor called? - CORRECT ANSWER - It is called when an instance of the
object is created, and memory is allocated for the object.
At object creation, the compiler calls constructors for all of its subobjects (its members and
inherited objects).
When are parameterized constructors called/not called? - CORRECT ANSWER - If
members have default constructors without parameters, those constructors are called
automatically, otherwise parameterized constructors are called using an initializer list.
Why is it called 'constructor'? - CORRECT ANSWER - Because it constructs the values at
the time of object creation
Is it necessary to write a constructor for a class? - CORRECT ANSWER - It is not,
because Java compiler creates a default constructor if your class doesn't have any.
, Rules to remember while creating a contructor - CORRECT ANSWER - Three rules:
- Constructor name must be the same as the class name
- Constructor must not have an explicit return type
- Java constructors cannot be abstract, static, final, or synchronized
What are the two types of constructors? Define each. - CORRECT ANSWER - - Default: a
no-args constructor that Java compiler inserts on your behalf
- Parameterized: has a specific number of parameters
Define the components of default constructors. - CORRECT ANSWER - they contain a
default call to super(), which is the default behavior.
Ex.
class Bike {
Bike() { }
...
}
Why are parameterized constructors used? - CORRECT ANSWER - used to provide
different values to the distinct objects in addition to the default values.
Ex.
class Student {
Student(int i, String n) {
id = i;
name = n;
}
...
}