Q & A in Java basics Part-1
1. What is Java?
Java is a widely used, high-level programming language known for
its portability and ability to run on multiple platforms.
2.
What are the key features of Java?
Java is known for its platform independence, object-oriented
nature, robustness, security, and automatic memory management.
3.
How do you define a class in Java?
To define a class in Java, you use the "class" keyword
followed by the class name and curly braces containing class members
4. What is a constructor
in Java?
A constructor is a special method used to initialize the
object's state when it is created. It has the same name as the class.
5.
How do you create an object in Java?
To create an object in Java, you use the "new" keyword
followed by the constructor of the class as shown below:
6. What is the difference
between an instance variable and a class variable?
An instance variable is associated with an instance of a class
and has a separate value for each object. A class variable, also known as a
static variable, is shared among all objects of the class.
7.
Explain the main method in Java.
8. How do you print output to the console in Java?
9. What are Java data
types?
Java has two categories of data types: primitive data types
(e.g., int, double) and reference data types (e.g., String, Array).
10.
Explain the difference between "==" and
".equals()" for comparing objects in Java.
"==" compares object references, while
".equals()" compares the content of objects. In most cases, you
should use ".equals()" to compare objects for equality.
11.
What is a package in Java?
A package is a collection of related classes and interfaces. It
helps organize code and provides access control.
12.
How do you import a package in Java?
You use the "import" statement followed by the package
name at the top of your Java file to import a package as shown in the following
figure:
13. What is method
overloading in Java?
Method overloading allows you to have multiple methods with the
same name in a class, but with different parameter lists.
14.
What is method overriding in Java?
Method overriding occurs when a subclass provides a specific
implementation for a method that is already defined in its superclass.
15.
How do you handle exceptions in Java?
Exceptions are handled using try-catch blocks. The code that
might raise an exception goes inside the "try" block, and the
exception handling code goes inside the "catch" block.
16. What is the purpose of the "finally" block in exception handling?
The "finally" block is used to
specify code that should be executed regardless of whether an exception occurs
or not.
17. What is a static method in Java?
A static method belongs to the class rather than an instance of the class. It can be called using the class name.
18. How do you declare constants in Java?
You can declare constants
using the "final" keyword.
19. Explain the
"this" keyword in Java.
The "this" keyword refers to the current instance of
the class and is often used to distinguish between instance variables and
method parameters with the same name.
20.
What is method chaining in Java?
Method chaining is a technique where you call multiple methods
on an object in a single line by returning the object from each method.
21. What is a static block in Java?
A static block is a block of code that runs
when the class is loaded into memory, before the creation of any object.
Comments
Post a Comment