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

_JAVA_ques

Jav questions

Uploaded by

shuvronil562
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

_JAVA_ques

Jav questions

Uploaded by

shuvronil562
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA ques

Is the constructor inherited?


No, constructors are not inherited because they are specific to the class they
belong to.

Can you make a constructor final?


No, constructors cannot be final because they are not inherited or overridden.

Can we overload the constructors?


Yes, constructors can be overloaded by changing their parameters.

Differences between constructors and methods?


Constructors initialize objects and have no return type; methods perform
actions and usually have a return type.

What is the static variable?


A static variable is shared across all instances of a class and belongs to the
class rather than any object.

What is the static method?


A static method belongs to the class, not objects, and can be called without
creating an object.

Restrictions on static methods?


They cannot access non-static variables or methods directly.

Why is the main method static?


It allows the JVM to call it without creating an instance of the class.

What is the static block?


A static block is used to initialize static variables and executes when the class
is loaded.

Can we execute a program without the main() method?


In modern Java versions, a main() method is mandatory to start execution.

JAVA ques 1
What if the static modifier is removed from the main method?
The program will not run as the JVM cannot invoke a non-static method.

Difference between static method and instance method?


Static methods belong to the class and cannot access instance variables,
while instance methods belong to objects.

Can we make constructors static?


No, constructors cannot be static.

Can abstract methods be static?

No, abstract methods cannot be static because static methods cannot be


overridden.

Can static variables/methods exist in an abstract class?


Yes, static members can be declared in abstract classes.

What is this keyword?


It refers to the current instance of the class.

Uses of this keyword?

Refer to current class members.

Invoke current class constructor.

Pass the current object as an argument.

Can this refer to static members?

No, this refers to the current instance, and static members belong to the
class.

Why is inheritance used in Java?

To promote code reusability and establish a relationship between classes.

Difference between overloading and overriding?

Overloading occurs within the same class with different parameters;


overriding occurs in a subclass with the same method signature.

Difference between abstract class and interface?

JAVA ques 2
Abstract classes allow method implementations; interfaces allow multiple
inheritance and have all methods as abstract by default (before Java 8).

Difference between class and interface?

A class can contain both concrete and abstract methods, while an interface
only contains abstract methods (before Java 8).

Can I overload the main method?


Yes, but the JVM always calls the main method with String[] args .

Can we override the main method?


No, it is static and hence not part of the object lifecycle.

Can an abstract method be final?


No, because it must be overridden.

Must a class be abstract if it contains one abstract method?

Yes, such a class must be declared abstract.

Can I create an instance of an abstract class?

No, but you can instantiate a subclass.

Similarities between abstract class and interface?

Both cannot be instantiated and are used to define abstract methods.

Can I create an object of a final class?

Yes, but it cannot be subclassed.

Can a final method be overridden?

No, a final method cannot be overridden.

Difference between abstraction and encapsulation?


Abstraction hides implementation details; encapsulation restricts direct access
to data.

Difference between throw and throws?


throwis used to explicitly throw an exception; throws declares exceptions a
method might throw.

JAVA ques 3
Difference between exception and error?
Exceptions can be recovered from; errors generally cannot.

Examples of exceptions:

NullPointerException

ArrayIndexOutOfBoundsException

Name of the Exception class?

java.lang.Exception .

Difference between checked and unchecked exceptions?

Checked exceptions are checked at compile time; unchecked are not.

What are try, catch, and finally blocks?

try contains code that might throw exceptions.

catch handles exceptions.

finally executes regardless of exceptions.

Difference between final and finally ?

final is a keyword; finally is a block for cleanup code.

What is a thread?
A thread is the smallest unit of a process.

Why is a thread called lightweight?


Threads share memory and resources of the process.

Difference between thread and process?

A process is independent; threads share memory within the same process.

What is the concurrency problem?

It arises when multiple threads access shared resources simultaneously


without proper synchronization, leading to inconsistent behavior.

Use of isAlive() function?

It checks if a thread is alive (running or ready to run).

JAVA ques 4
Methods for threads:

run() : Defines the thread's execution code.

start() : Begins a thread’s execution by calling run() .

sleep(long milliseconds) : Pauses the thread for a specified time.

join() : Waits for a thread to finish execution.

join(long milliseconds) : Waits for a thread for the specified time.

getPriority() : Returns the thread's priority.

setPriority(int priority) : Sets the thread's priority.

getName() : Retrieves the thread's name.

setName(String name) : Sets the thread's name.

currentThread() : Returns a reference to the currently executing thread.

getId() : Returns the thread's ID.

yield() : Suggests the scheduler to give other threads a chance to execute.

suspend() : Deprecated; pauses the thread.

resume() : Deprecated; resumes a paused thread.

stop() : Deprecated; stops a thread abruptly.

Two ways to implement threads:

Extend the Thread class.

Implement the Runnable interface.

What is synchronization, and why use it?

Synchronization ensures thread-safe access to shared resources to prevent


concurrency issues.

Can you start a thread twice?


No, once a thread has finished, it cannot be restarted.

Method called before run() for initialization?


The start() method.

JAVA ques 5
Method used to register a thread in a thread scheduler?
The start() method.

What is thread priority in Java?

A thread's priority determines its importance in the scheduler.

Can I write main() without String[] args ?

No, the signature must include String[] args for the JVM to recognize it.

Instance vs. local variable:

Instance variables belong to objects; local variables are defined within


methods and limited to their scope.

Difference between length() and length :

length() is a method for strings.

length is a property for arrays.

Use of super keyword:

Refers to the parent class and can access its methods, variables, and
constructors.

Can static methods be overloaded?


Yes, by changing the parameter list.

What is JVM?
The Java Virtual Machine executes Java bytecode.

What is JDK?

The Java Development Kit contains tools and libraries for Java development.

What happens with multiple main() methods in a class?


The JVM calls the one with the correct signature ( public static void main(String[]

args) ).

Can you call one constructor inside another?

Yes, using this() .

Importing the same class/package twice:

JAVA ques 6
It is allowed, but the JVM ignores duplicates.

What is WORA in Java?

"Write Once, Run Anywhere" – Java's platform independence.

Features of Java:

Object-oriented

Platform-independent

Robust and secure

Multithreaded

High performance (due to JIT compiler)

JAVA ques 7

You might also like