0% found this document useful (0 votes)
27 views

OOPs - Exam

The document discusses several key concepts in object-oriented programming (OOP) and Java: 1. It defines classes, objects, abstraction, polymorphism, inheritance, and encapsulation as the main OOP features. It explains that classes are templates for objects and objects are instances of classes. 2. It describes bytecode as the low-level representation of compiled Java code that is executed by the Java Virtual Machine (JVM) and provides platform independence, security, and portability. 3. It differentiates between method overloading and overriding, and explains that the 'super' keyword is used to access superclass members and invoke superclass constructors.

Uploaded by

souviklap7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

OOPs - Exam

The document discusses several key concepts in object-oriented programming (OOP) and Java: 1. It defines classes, objects, abstraction, polymorphism, inheritance, and encapsulation as the main OOP features. It explains that classes are templates for objects and objects are instances of classes. 2. It describes bytecode as the low-level representation of compiled Java code that is executed by the Java Virtual Machine (JVM) and provides platform independence, security, and portability. 3. It differentiates between method overloading and overriding, and explains that the 'super' keyword is used to access superclass members and invoke superclass constructors.

Uploaded by

souviklap7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1. Discuss the features of Java with respect to OOP. _____ What is Byte code?

Q 1…..>>
 Object Oriented Programming (OOP) is a programming paradigm that
focuses on the use of objects to represent and manipulate data. In OOP, data is
encapsulated within objects, and objects are defined by their properties
(attributes) and behaviours (methods). OOP provides several key concepts that
enable developers to write modular, reusable, and maintainable code.
Explain the features Of Object Oriented Programming.
The main features of object oriented programming are as follows:
i) Classes
ii) Objects
iii) Abstraction
iv) Polymorphism
v) Inheritance
vi) Encapsulation
i) & ii) Classes & Objects in OOPs : A class is a template that consists of the data
members or variables and functions and defines the properties and methods for a
group of objects. The compiler does not allocate memory whenever you define a class.
An object is nothing but an instance of a class. Each object has its values for the
different properties present in its class. The compiler allocates memory for each object.
iii) Abstraction: The literal meaning of abstraction is to remove some characteristics
from something to reduce it to a smaller set. Similarly, Object Oriented Programming
abstraction exposes only the essential information of an object to the user and hides
the other details.

vi) Polymorphism: The word polymorphism means to have many forms. So, by using
polymorphism, you can add different meanings to a single component.
There are two types of polymorphism:
i) Run-time polymorphism ii)Compile-time polymorphism
v) Inheritance: Inheritance is one of the most important features of object oriented
programming. It allows a class to inherit the properties and methods of another class
called the parent class, the base class, or the super-class. The class that inherits is called
the child class or sub-class. It helps to avoid duplication of codes by allowing code
reuse as you need not define the same methods and properties present in a super-class
in the sub-classes again. The sub-class can simply inherit them.
vi) Encapsulation: Encapsulation means enclosing the data/variables and the
methods for manipulating the data into a single entity called a class. It helps to hide
the internal implementation of the functions and state of the variables, promoting
abstraction.
Q 2…..>>
Bytecode, in the context of Java programming, is a low-level representation of
compiled Java source code. When you write Java code, it is first compiled into an
intermediate form known as bytecode by the Java compiler. Bytecode is a set of
instructions that are designed to be executed by the Java Virtual Machine (JVM).

Page | 1
Key characteristics of bytecode:
Platform-Independent: Bytecode is platform-independent, which means that it
can beexecuted on any system that has a Java Virtual Machine (JVM) installed.
Intermediary Form: Bytecode serves as an intermediary representation between
the high-level Java source code and the machine-specific native code.
Security: Bytecode provides a layer of security since the JVM can enforce access
controls, manage memory, and prevent potentially unsafe operations.
Portability: The portability of bytecode is a fundamental advantage for web-based
applications (applets) and distributed systems, as it allows the same Java application
to run on different client machines without modification.
Just-In-Time Compilation: In some cases, bytecode is further compiled into native
machine code by the JVM's Just-In-Time (JIT) compiler, which can improve
performance.

2. What is the difference between overloading and overriding methods in


Java? State theuse of super keyword.
 The key difference between overloading and overriding methods in Java is:
Overloading: Involves having multiple methods with the same name in the same
class, but with different parameters. It's resolved at compile time and provides
versatility and convenience.
Overriding: Occurs when a subclass provides a specific implementation for a
method inheritedfrom its superclass. The method in the subclass has the same name,
return type, and parameter list as the one in the superclass. It's resolved at runtime
and achievespolymorphism.
Q 2…..>>
The `super` keyword in Java is used to:
i. Access superclass members in a subclass.
ii. Invoke superclass constructors, particularly when subclass constructors require
additionalsetup.
iii. Prevent method override by marking a method as `final`.
iv. Temporarily treat an object as an instance of its superclass.

3. Differentiate between final, finally, and finalize in Java.


 In Java, "final," "finally," and "finalize" are three different concepts, each serving
distinctpurposes:
Final:
 final is a keyword used to define various entities as unchangeable or not
extendable.
When applied to a variable, it makes the variable a constant that cannot be
reassignedonce initialized.
 When applied to a method, it prevents that method from being
overridden insubclasses.
 When applied to a class, it makes the class non-extendable, and it cannot be
subclassed.

Page | 2
Finally:
 finally is a block used in exception handling to ensure that a certain block
of code getsexecuted regardless of whether an exception is thrown or not.
 It is typically used in a try-catch-finally block to perform cleanup
operations, such asclosing resources like files or network connections.
Finalize:
 finalize is a method in Java that is used for cleaning up and releasing
resources beforean object is garbage collected.
 It is part of the Java Object class and can be overridden in user-defined classes
to specifycleanup operations.
 It is, however, considered somewhat outdated, as modern Java programming
relies on more deterministic resource management using methods like try-
with-resources forresource cleanup.

4. Why multitable inheritance is done in java?

 In Java, multitable inheritance is not a common term, and it's not a standard
feature provided by the Java language itself. Instead, you might be referring
to a design pattern or a specific programming technique used to achieve
inheritance with multiple tables in a relational database, commonly known as
"table per class hierarchy" or "table per subclass."

When working with object-oriented programming and relational databases, you


often need to map your Java class hierarchy to database tables. There are different
strategies for achieving this, and one of them is the use of multiple tables to
represent inheritance hierarchies. This is typically done when you have a
superclass and multiple subclasses, each of which is mappedto a separate database
table.

5. Why the main function is declared as static? What do you mean by abstract
class and abstract method?
 In Java, the main function is declared as static for a specific reason:
Entry Point: The main method is the entry point of a Java program. When you run
a Java application, the Java Virtual Machine (JVM) needs to start executing the
program without creating an instance of the class. In other words, it should be able
to call the main method without constructing an object of the class containing it.
Static Binding: When a program starts, the JVM loads the class and looks for the
public static void main(String[] args) method. Since there is no instance of the class
at this point, the method needs to be associated with the class itself, rather than an
instance. This is where the static keyword comes in.
Single Entry Point: Having a static main method ensures that there is only one
entry point to the program. Without it, you could have multiple entry points
depending on how many instances of the class you create.

Page | 3
Q 2…..>>
In Java, an abstract class and an abstract method serve as key concepts in object-
oriented programming to design and structure classes and methods. Here's what
each term means:

Abstract Class:
 An abstract class is a class that cannot be instantiated (i.e., you cannot create
objects ofit).
 It is typically used as a base class for other classes (concrete subclasses) and
serves as ablueprint for those subclasses.
 An abstract class can contain a mix of abstract (unimplemented) methods
and concrete(implemented) methods.
 Subclasses of an abstract class are required to provide implementations for
all abstractmethods defined in the abstract class.

Abstract Method:
 An abstract method is a method declared in an abstract class but without
animplementation in the abstract class.
 Abstract methods are marked with the abstract keyword and end with
a semicoloninstead of a method body.
 Subclasses that inherit from an abstract class must provide concrete
(implemented)definitions for all the abstract methods declared in the abstract
class.

6. “Java Is a platform depended language.”- Justify the statement. What is


“super” and “this” keyword? What is final modifier?
Q 1…..>>
 Here's the justification for this statement:
Platform Independence: Java was specifically developed to be platform
independent. It achieves platform independence through the Java Virtual Machine
(JVM), Bytecode.
Write Once, Run Anywhere: The Java philosophy of "write once, run anywhere"
means that you can write Java code on one platform and then run it on any other
platform that has a compatible JVM. The bytecode ensures that the same Java
program can execute on Windows, macOS, Linux, or any other platform with a JVM.
Portability: The Java language and runtime environment have been designed to
abstract away platform specific details. Java libraries and APIs provide consistent
and portable ways to access system resources, making it easier to write platform
independent code.
Security Model: Java's platform independence enhances security by running
applications within a sandboxed environment. It prevents untrusted code from
causing harm to the host system.

Page | 4
Q 2….>>
In Java, "super" and "this" are special keywords used to refer to different elements
within a class. Here's what each keyword means and how it is used:
This Keyword:
 this is a reference to the current instance of the class in which it is used. It is
primarily used to avoid naming conflicts between instance variables (class
fields) and method parameters or local variables.
 It can be used to refer to instance variables, instance methods, or constructors
within the same class.
 For example, if you have a method parameter or a local variable with the
same name as an instance variable, you can use this to clarify that you're
referring to the instance variable.
Super Keyword:
 super is a reference to the superclass (parent class) of the current class. It is
used to access or call superclass members (fields, methods, or constructors)
that might beoverridden in the subclass.
 It is typically used within a subclass to invoke the superclass's constructor or
access methods or fields of the superclass.
Q 3……>>
The final modifier in Java is used to restrict or modify the behavior of classes,
methods, andvariables. When applied to different elements, it has different meanings:
Final Variable:
 When applied to a variable, the final modifier makes it a constant. The
variable's valuecannot be changed once it has been assigned a value.
 Example: final int constantValue = 10;
Final Method:
 When applied to a method, the final modifier indicates that the method
cannot be overridden or extended by subclasses. Subclasses are not allowed
to provide a different implementation for a final method.
Final Class:
 When applied to a class, the final modifier indicates that the class cannot be
extended. You cannot create subclasses of a final class.

7. Describe the life cycle of Applet. ____ What is wrapper class?


Q 1……>>
 The life cycle of a Java applet, a small program that runs in a web browser,
includes:
1. Initialization: The init method is called for one-time setup.
2. Starting: The start method begins or resumes the applet's execution.
3. Running: The applet continues to run while it's visible in the web page.
4. Stopping: The stop method is called when the applet is no longer visible.
5. Destroying: The destroy method releases resources when the applet is
removed.
6. Termination: After destroy, the applet's life cycle ends.

Page | 5
Q 2…..>>
A wrapper class in Java is a class that allows you to represent primitive data types
(like int, char, boolean, etc.) as objects. It "wraps" the primitive value in an object,
providing utility methods for working with these values. The primary purpose of
wrapper classes is to enable object-oriented features like inheritance, and to provide
additional functionality, such as converting between data types and performing
various operations on the values.

In Java, there are eight primitive data types, and each has a corresponding wrapper
class:
1. int - Integer
2. char - Character
3. boolean - Boolean
4. byte - Byte
5. short - Short
6. long - Long
7. float - Float
8. double – Double

8. How will you call the parameterized call and override method from the parent
class in the subclass ?
 Here's the algorithm to call a parameterized constructor and an overridden
method from theparent class in the subclass in Java:
Calling a Parameterized Constructor from the Subclass:
 Create the parent class (superclass) with a parameterized constructor that
acceptsarguments.
 Create the subclass (child class) that extends the parent class.
 In the subclass's constructor, use the super keyword followed by the
appropriate constructor arguments to call the parent class's parameterized
constructor.
Calling an Overridden Method from the Subclass:
 Create the parent class with a method that you intend to override in the
subclass.
 Create the subclass that extends the parent class.
 In the subclass, create a method with the same name as the method in the
parent classthat you want to override.
 Inside the overridden method in the subclass, use the super keyword
followed by themethod name to call the parent class's implementation of the
method.
 Add the subclass-specific behavior as needed.

Page | 6
9. In Java explain how to call a constructor from another. Indicate the difference
between the path and the clean path.
Q 1…..>>
 In Java, you can call one constructor from another constructor within the
same class using the this keyword. This is known as constructor chaining,
and it can be useful when you want to reuse code or set default values for
certain constructor parameters. Here's how you can call a constructor from
another in Java:
 Create a class with multiple constructors.
 In one constructor, use the this keyword followed by the appropriate
constructorparameters to call another constructor within the same class.
Q 2…..>>
Here's the difference between path and clean path:
Path:
 "Path" refers to a list of directories or folders that specify the locations
where theoperating system or a program should search for executable files
or resources.
 In the context of the operating system, the "PATH" environment variable
contains a listof directories that the system uses to find executable programs.
Clean Path:
 "Clean path" is not a standard computing term. However, it could refer to a
pathvariable that has been modified or sanitized to remove duplicates or
incorrect entries. Cleaning the path may involve removing redundant or
invalid directory entries to ensurethat the search for files and resources is
more efficient and accurate.
 For example, in a programming context, you might clean a path by removing
any duplicate directory entries or ensuring that the path is well-formed and
free of errors.

10. “Can a super class object reference a subclass object?”- Explain.


 No, a superclass object cannot directly reference a subclass object in Java. In
Java, object references are strongly typed, which means an object reference
can only refer to objects of its declared type or its subtypes (polymorphism).
Here's a detailed explanation:
 Superclass-Subclass Relationship: In Java, a subclass inherits from a
superclass. This means that an object of the subclass contains both the fields
and behaviors of the superclass, but it is not the other way around. A
superclass object does not inherit the fields and behaviors of its subclasses.
 Object Reference Types: When you declare an object reference variable, its
type determines the methods and fields you can access on that object. For
example, if you have a superclass Vehicle and a subclass Car, an object of type
Vehicle can only access methods and fields defined in the Vehicle class.
 Polymorphism: While a superclass reference cannot directly access subclass-
specific methods or fields, Java supports polymorphism. If you have a
reference to a superclass (Vehicle) that is actually pointing to an object of a

Page | 7
subclass (Car), you can call methods that are overridden in the subclass.
However, you won't be able to access methods or fields that are specific to
the subclass (not in the superclass).

11. What is the difference between ‘integer’ and ‘Integer’ keyword?

 Here's the difference between integer and integer keyword:


integer (with a lowercase "i"):
 "Integer" is not a recognized keyword in Java. It seems to be a regular
lowercase word. Ifused in Java code, it would be interpreted as an identifier
(a variable or class name) and not as a special keyword.
 For example, you can define a variable with the name "integer"
in your code:int integer = 42; // Valid code
Integer (with an uppercase "I"):
 "Integer" is a valid class in Java that belongs to the java.lang package. It is a
wrapper class for the primitive data type int. Wrapper classes are used to
work with primitive data types as objects and provide useful methods for
data conversion and manipulation.
 You can use the "Integer" class to create objects that represent integer values,
and it provides methods to work with those objects.
 Example, code:
Integer number = new Integer(42); // Create an
Integer objectint value = number.intValue(); //
Convert it to an int

12. How does any application differ from an applet? What is meant by
local applet andremote applet? What is privet constructor?

 Q 1…..>>
Execution Context:
 Applications run as standalone programs on the user's local machine.
 Applets are embedded in web pages and executed within web browsers.
User Interface:
 Applications often have full graphical user interfaces (GUIs) with windows
and dialogs.
 Applets have more limited GUI capabilities, typically confined to the applet's
embedded area within a web page.
Entry Point:
 Applications have a main method as their entry point.
 Applets do not have a main method but rely on lifecycle methods like init,
start, stop,and destroy.

Page | 8
Access to System Resources:
 Applications have relatively unrestricted access to system resources,
including file I/Oand network access.
 Applets run in a sandboxed environment within the browser and have
restricted accessto system resources.
Control and Lifecycle:
 Applications are fully controlled by the user, who can start, stop, and interact
with themas desired.
 Applets are initiated when a web page is loaded and are controlled by the
web browser.Users interact with applets within the web page.
Distribution:
 Applications are distributed as standalone executable files or packages.
 Applets are distributed as part of web pages and run within the context of
those pages.
Q 2…….>>
"Local applet" and "remote applet" refer to where the applet is loaded from and
executed:
Local Applet:
 A local applet is one that is loaded and executed from the local file system or
a local server. It is essentially a Java applet residing on the user's machine or
a nearby server.
 Local applets are typically used for testing and development purposes or in
situations where you have direct control over the hosting environment.
Remote Applet:
 A remote applet is loaded and executed from a remote web server over
the internet.The applet's code and resources are fetched from a web server
when a web page containing the applet is accessed.
 Remote applets are common when you want to deliver interactive and
dynamic functionality through web pages. Users can access these applets by
visiting a website that hosts the applet.
Q 3…….>>
A "private constructor" is a constructor within a Java class that is marked with the
private access modifier. Private constructors are not accessible from outside the
class, meaning they cannot be used to create instances of the class from other classes.
Private constructors are typically used for various purposes, including:
 Singleton Design Pattern: Private constructors can be used to implement
the singleton design pattern, ensuring that only one instance of a class is
created.
 Factory Methods: Private constructors can be used in conjunction with
public factorymethods to control object creation.
 Utility Classes: Private constructors can be used in utility classes that
contain only staticmethods and should not be instantiated.

Page | 9
13. Explain public, privet and protected modifiers in brief.
 The public, private, and protected modifiers in Java are used to control the
accessibility andvisibility of classes, methods, and fields within a class. Here's
a brief explanation of each: Public: Public members are accessible from
anywhere, both within and outside the class and inany package.
Private: Private members are only accessible within the same class where they are
defined,making them hidden from outside access.
Protected: Protected members are accessible within the same class, its subclasses
(even in different packages), and within the same package, offering a level of access
control between private and public.

Page | 10

You might also like