Unit-1 Questions
I. Introduction to Java
A. Core Concepts (JVM, Platform Independence)
What is the Java Virtual Machine (JVM)? Discuss its advantages.
What is the difference between the Java Development Kit (JDK) and the JVM?
Define "platform independence." What makes Java platform-independent? Explain in detail.
Can Java run on any machine? Justify your answer.
B. Class Fundamentals
Explain the core concepts of Object-Oriented Programming (OOP) in Java: Encapsulation, Inheritance, and Polymorphism.
Explain each word in the standard Java main method signature:
public static void main (String args[])
.
C. Declaring Objects, Assigning Object Reference Variables
What is a class? What is an object? Explain the difference between a class and an object with an example.
How do you define a class and create objects from that class in Java? Provide an illustrative example.
What are access control modifiers (access specifiers) supported by Java? Explain the different types (e.g., public, private, protected, default/package-private) and illustrate their usage and effect on class members with an example.
D. Introducing Methods, Constructors
Define a constructor. Discuss its special properties and advantages.
Explain how constructors can be overloaded with the help of an example.
Programming Exercise: Create a
Swapper
class with:- Two integer instance variables,
x
andy
. - A constructor that takes two integer parameters to initialize
x
andy
. - A
getX()
method that returns the value ofx
. - A
getY()
method that returns the value ofy
. - A
void swap()
method that swaps the values ofx
andy
. Then, create aSwapperDemo
class to test all the methods of theSwapper
class.
- Two integer instance variables,
Programming Exercise: An
Employee
class contains the following members:- Data members:
emp_no
,emp_name
,basic
,da
,IT
,Net_sal
. - Member functions: To initialize data, to calculate net salary, and to print data members.
Write a Java program to read the data for an employee and compute their net salary. The
DA
(Dearness Allowance) is 60% of thebasic
pay. TheIT
(Income Tax) is 20% of thebasic
pay if thebasic
pay is less than 1,500,000, otherwise, it is 30% of thebasic
pay.Net_sal
=basic
+da
-IT
.- Data members:
E. The this
Keyword, Garbage Collection, The finalize()
Method
Discuss the use and purpose of the
this
keyword in Java.What is Garbage Collection in Java?
What does the
finalize()
method do? Explain its role with an example in the context of garbage collection.
F. Exploring the String
Class, Using Command-Line Arguments
- Write a Java program to find the area of a rectangle using command-line arguments to pass the length and width.
G. Varargs, Scanner
Class
What are variable-length arguments (varargs) in Java? What are the advantages of using them?
Illustrate the use of varargs with a programming example.
Discuss the usage of the
Scanner
class in Java for input operations.
II. Inheritance
A. Inheritance Basics
What is inheritance in Java?
What are the advantages of using inheritance?
Provide an example of inheritance in Java.
Illustrate inheritance with an example, specifically showing the effect of
private
anddefault
(package-private) access modifiers on inherited members.
B. Using super
, Creating a Multilevel Hierarchy
Describe the use of the
super
keyword in the context of inheritance. Provide an example.Illustrate with an example how to invoke a superclass constructor from its subclass using
super()
.Explain the order of constructor execution in a multilevel hierarchy of classes with an example.
C. When Constructors Are Called, Method Overriding
Define method overriding and method overloading. Illustrate both concepts with Java code examples.
What are the rules for method overriding in Java?
How is dynamic polymorphism (runtime polymorphism) achieved through method overriding? Provide an example.
D. Dynamic Method Dispatch
What is Dynamic Method Dispatch in Java?
Illustrate Dynamic Method Dispatch (also known as runtime polymorphism) with a programming example.
E. Using Abstract Classes
What is an abstract class in Java? Provide an example.
Explain the syntax and use of an
abstract
class.Programming Exercise: Create an abstract class named
Shape
. Create two subclasses,Rectangle
andTriangle
, that extendShape
. Include appropriate methods in both subclasses to calculate and return their respective areas.
F. Using final
with Inheritance
What is the purpose of the
final
keyword in Java? Explain and provide code examples for its three primary uses:final
variables (constants)final
methods (cannot be overridden)final
classes (cannot be extended)
Explain the role and significance of the
java.lang.Object
class in the Java class hierarchy.