Professional Documents
Culture Documents
Oop Final Exam
Oop Final Exam
Or,
Q-2:In JAVA programming language how many of Primitive Data Types we
have? Briefly Explain whatever you know about them.
Answer: there are 8 primitive data types in Java:
• byte - A byte is an 8-bit signed integer. It can store values from -128 to
127.
• short - A short is a 16-bit signed integer. It can store values from -32,768
to 32,767.
• int - An int is a 32-bit signed integer. It can store values from -
2,147,483,648 to 2,147,483,647.
• long - A long is a 64-bit signed integer. It can store values from -
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
• float - A float is a 32-bit floating-point number. It can store values with a
precision of up to 7 decimal places.
• double - A double is a 64-bit floating-point number. It can store values
with a precision of up to 15 decimal places.
• boolean - A boolean is a 1-bit data type. It can store only two values: true
or false.
• char - A char is a 16-bit unsigned integer. It can store a single character
from the Unicode character set.
Q-3:Write down the output of following code:
a) public class Main { public static b)
void main(string args[]) public class Main (
int digit = 47{; public static void main (String args[])
digit = 65; { int Hello World= 1111;
System.out.println(digit); System.out.println("Hello World");
} System.out.println("Welcome To
MEC");
//System.out.println(Hello World);
System.out.println("Hello World");
Answer:
a)
b)
Q-4:How you can assign object a reference variable? Give proper
explanation.
Answer:
In most programming languages, including Java, you can assign an object to a
reference variable using the following steps:
1. Declare the reference variable: Start by declaring a variable of the
appropriate type that will hold the reference to the object. For example, if
you have a class called "Person", you can declare a reference variable of
type "Person" like this: Person personObj;
2. Create an instance of the object: Use the new keyword followed by the
constructor of the class to create a new instance of the object. For
example, to create a new instance of the "Person" class, you can use:
personObj = new Person();
3. Assign the reference: Finally, assign the reference of the newly created
object to the reference variable using the assignment operator (=). This
establishes a connection between the reference variable and the object in
memory. For example: personObj = new Person();
Here's a complete example:
public class Person {
private String name;
• Rename the instance variable in the subclass. This is the simplest way to
solve the problem of instance variable hiding, but it can make the code
more difficult to read and understand.
Here is an example of how to rename the instance variable in the subclass:
Q-6:What is method overloading? Explain with proper example.
Answer
Method overloading is a feature in object-oriented programming languages that
allows multiple methods with the same name but different parameters to be
defined within a class. The compiler determines which method to invoke based
on the number, type, and order of the arguments passed during the method call.
Q-7:Write the correct output of the following segment:
class Box {
int width; float height; double depth;
class boxDemo ( public static void main(string args[]) {
Box mybox = new Box();
double vol();
mybox.width = 10.00;
mybox.height = 20;
mybox.depth = 15.50;
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
Answer:
class Box {
int width;
float height;
double depth;
Output:
Q-8: What is inheritance? What do you know about the classification of it?
Answer:
Inheritance is a powerful feature in object-oriented programming that allows
one class to inherit the properties and methods of another class. This means that
the subclass can reuse the code from the superclass, which can save time and
effort when developing new classes.
There are four main types of inheritance in Java:
• Single inheritance: A subclass can inherit from only one superclass.
• Multilevel inheritance: A subclass can inherit from another subclass,
which inherits from a superclass.
• Hierarchical inheritance: A subclass can inherit from multiple subclasses,
which all inherit from a single superclass.
• Multiple inheritance: A subclass can inherit from multiple superclasses.
Inheritance is a powerful tool that can be used to improve the readability,
maintainability, and extensibility of code. However, it is important to use
inheritance carefully to avoid errors.
Here are some of the benefits of inheritance:
• Reusability: Inheritance allows you to reuse code from existing classes,
which can save time and effort when developing new classes.
• Readability: Inheritance can make code more readable by making it easier
to understand the relationships between different classes.
• Maintainability: Inheritance can make code more maintainable by making
it easier to change existing classes without affecting other classes.
• Extensibility: Inheritance can make code more extensible by making it
easier to add new features to existing classes.
Q-9: Why java class doesn’t support multiple inheritance but java interface
supports this?
Answer: Java does not support multiple inheritance for classes because it can
lead to ambiguity. Interfaces, on the other hand, do support multiple inheritance
because they only contain abstract methods.
Here are some of the reasons why Java does not support multiple inheritance for
classes:
• Ambiguity: As mentioned earlier, multiple inheritance can lead to
ambiguity if two classes have the same method with the same name. The
compiler will not know which method to call, which can lead to errors
and confusion.
• Complexity: Multiple inheritance can make code more complex,
especially if the classes involved have a lot of methods. This can make it
difficult to understand and maintain the code.
• Performance: Multiple inheritance can have a negative impact on
performance, especially if it is used to create deep hierarchies of classes.
This is because the compiler needs to keep track of all the methods that
are inherited from each class, which can slow down the execution of the
code.
Here are some of the reasons why Java supports multiple inheritance for
interfaces:
• Flexibility: Multiple inheritance allows classes to inherit from multiple
interfaces, which gives them more flexibility. This can be useful for
creating classes that need to implement the features of multiple different
interfaces.
• Reusability: Multiple inheritance can help to improve the reusability of
code. This is because classes can inherit from interfaces that are already
implemented, which saves time and effort.
• Performance: Multiple inheritance does not have a negative impact on
performance, as interfaces only contain abstract methods. This means that
the compiler does not need to keep track of all the methods that are
inherited from each interface, which can speed up the execution of the
code.
Overall, Java does not support multiple inheritance for classes because it can
lead to ambiguity. However, interfaces do support multiple inheritance because
they only contain abstract methods. This makes interfaces more flexible,
reusable, and efficient than classes.
Q-10: Note down the comparison method overloading and method
overriding .
Answer:
Definition A method overloading A method overriding occurs
occurs when a class has when a subclass has a
multiple methods with the method with the same name,
same name but different signature, and return type as
parameters. a method in its superclass.
Compile-time Method overloading is a Method overriding is a
polymorphism compile-time polymorphism runtime polymorphism
because the compiler because the Java Virtual
decides which method to Machine (JVM) decides
call based on the arguments which method to call at
that are passed to the runtime based on the type of
method. the object that is being called.
Signature The signatures of The signatures of overriding
overloaded methods must be methods must be the same.
different. This means that This means that the name, the
the number and/or type of number, and the type of the
the parameters must be parameters must be the same.
different.
Return type The return type of The return type of overriding
overloaded methods can be methods must be the same as
the same or different. the return type of the method
in the superclass.
Usage Method overloading is used Method overriding is used to
to provide different provide a more specific
implementations of the same implementation of a method
method for different types of in the superclass.
arguments.
Q-1: What are the Basic Feature Of OOP?
Answer:
Q-3: Create a class name ‘Student’ with String variable ‘name’ and integer
variable ‘roll no’. Assign the value the roll number as ‘2’ and that of name
as ‘John’ by creating by object of the class student.
Answer:
Q-4: Define Constructor. Difference between Method & Constructor.
Answer:
Definition Constructor: A constructor in Java is a special method that is used
to initialize objects. The constructor is called when an object of a class is
created.
Example:
public class Main {
int x;
public Main () {
x = 5; // Set the initial value for the class attribute x
}
public static void main (String [] args) {
Main myObj = new Main (); // Create an object of class Main (This will call
the constructor)
System.out.println(myObj.x); // Print the value of x
}
}
// Outputs 5
The void return type in a constructor tells the compiler that the constructor does
not return any value. This is because constructors are used to initialize objects,
not to return values.
If you try to compile the code with the incorrect constructor, you will get the
following error message:
To fix the error, you need to change the return type of the constructor to void.
Once you have done this, the code will compile and run without any errors
Q- 6:What are the access specifiers in OOP? Write down the differences
among them.
Answer:
In object-oriented programming, a member access modifier is a keyword used
to define the accessibility or visibility of class members such as variables,
methods, and properties.
There are typically three levels of access modifiers:
Public: Members declared as public can be accessed from anywhere in the
program.
Private: Members declared as private can only be accessed within the same
class.
Protected: Members declared as protected can be accessed within the same class
and its derived classes.
সর্বজনীন: সর্বজনীন হিসাবর্ ঘ াহিত সদসযবদর ঘরাগ্রাবের ঘে ঘোনও জায়গা ঘেবে অ্যাবেস েরা ঘেবত পাবর।
র্যহিগত: র্যহিগত হিসাবর্ ঘ াহিত সদসযবদর শুধু োত্র এেই ঘেণীর েবধয অ্যাবেস েরা ঘেবত পাবর।
সু রহিত: সু রহিত হিসাবর্ ঘ াহিত সদসযবদর এেই ঘেণী এর্ং এর ঘেবে রাপ্ত ক্লাবসর েবধয অ্যাবেস েরা ঘেবত
পাবর।
class Animal {
public void makeSound() {
System.out.println("The animal makes a sound");
}
}
Q-8: What do you mean by abstract class? Why it is needed? Explain with
example.
Answer:
In Java, an abstract class is a class that cannot be instantiated and is designed to
be subclassed by other classes. An abstract class can contain both abstract and
non-abstract (concrete) methods, as well as instance variables, constructors, and
other features of a standard Java class.
An abstract class is needed because it allows you to define a common interface
or behavior for a group of related classes without having to repeat code in each
individual class. This makes your code more modular and easier to maintain
since changes made to the abstract class will automatically propagate to all of
its subclasses.
Here's an example of an abstract class in Java:
@Override
public void calculateArea() {
area = length * width;
}
@Override
public void calculatePerimeter() {
perimeter = 2 * (length + width);
}
}
Q-9: Why "this" keyword is used?
Write a program that takes two integers from keyboard to perform division
operation. A try block to throw an exception when a wrong type of data is keyed
or division by zero is occurred. Also write appropriate catch block to handle the
exception thrown.
Answer:
"this" keyword (uses):
The "this" keyword in Java is a reference to the current object within an
instance method or constructor. It is used to differentiate between instance
variables and method parameters or local variables that have the same name.
Here's an example to illustrate the usage of the "this" keyword:
In this code, the "this" keyword is used to refer to the "name" instance variable
of the current object. In the constructor, "this.name = name;" sets the value of
the "name" instance variable to the value passed in as a parameter. In the
"sayHello" method, "this.name" is used to get the value of the "name" instance
variable of the current object and print it out.
It's important to note that using "this" is optional in many cases, but can
improve code readability and reduce ambiguity, especially in cases where local
variables have the same name as instance varia
Here's an example program that takes two integers from the keyboard and
performs a division operation, using a try block to handle exceptions:
In this program, we use the Scanner class to read two integers from the
keyboard. The division operation is performed within the try block. If a division
by zero occurs, an ArithmeticException is thrown. If the user enters an invalid
input, such as a non-integer value, an InputMismatchException is thrown.
In Java generics, bounded types allow you to restrict the types that can be used
as type arguments in a generic class or method. There are two types of bounded
types: upper bounded types and lower bounded types.
1. Upper Bounded Types: upper bounded type is specified using the extends
keyword. It restricts the type argument to be a specific type or any of its
subclasses. For example, if we have a generic class Box<T extends
Number>, it means that the type argument T must a subclass of Number
or Number itself.
Here's an example of a generic class Box with an upper bounded type:
Now, you can instantiate the Box class with different types that extend Number,
such as Integer, Double, or Float. Here's how you can do it:
Lower Bounded Types: A lower bounded type is specified using the super
keyword. restricts the type argument to be a specific type or any of its
superclasses. For example, if we have a generic class Container<T super
String>, it means that the type argument T must be String a superclass of String.
Here's an example of a generic class Container with a lower bounded type:
Now, you can instantiate the Container class with different types that are
superclasses of String, such as Object or Serializable. Here's how you can do it:
Md Raihan ali_63
Cse_02
Faculty of Engineering & Technology
University of Dhaka (NITER)