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

JAVA Practice Questions

The document provides a comprehensive overview of Java programming concepts, including constructors, static methods, inheritance, exception handling, and threading. It explains key differences between abstract classes and interfaces, as well as between various types of exceptions. Additionally, it covers Java's features, such as portability and multithreading, and addresses common questions related to Java programming.

Uploaded by

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

JAVA Practice Questions

The document provides a comprehensive overview of Java programming concepts, including constructors, static methods, inheritance, exception handling, and threading. It explains key differences between abstract classes and interfaces, as well as between various types of exceptions. Additionally, it covers Java's features, such as portability and multithreading, and addresses common questions related to Java programming.

Uploaded by

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

Q1. Is constructor inherited?

No, The constructor is not inherited.

Q2. Can you make a constructor final?

No, the constructor can't be final.

Q3.Can we overload the constructors?

Yes

Q4. What are the differences between the constructors and methods?

Q5. What is the static variable?

AnsThe static variable is used to refer to the common property of all objects

Q6. What is the static method?

Q7. What are the restrictions that are applied to the Java static methods?

Ans The static method can not use non-static data member or call the non-static method directly. His and super cannot be used in
static context as they are non-static.

Q8. Why is the main method static?


Because the object is not required to call the static method. If we make the main method non-static, JVM will have to create its
object first and then call main() method which will lead to the extra memory allocation.

Q9. What is the static block?

Static block is used to initialize the static data member. It is executed before the main method, at the time of class loading.

Q10. Can we execute a program without main() method?

Ans) No,

Q11. What if the static modifier is removed from the signature of the main method?

Ans) Program compiles. However, at runtime, It throws an error "NoSuchMethodError."

Q12. What is the difference between static (class) method and instance method?

Q13. Can we make constructors static?

Ans) No, the compiler will show the compiler error.

Q14. Can we make the abstract methods static in Java?

Ans) No

Q15. Can we declare the static variables and methods in an abstract class?

Ans)Yes,

Q16. What is this keyword in java?

Q17. What are the main uses of this keyword?

Ans)this can be used to refer to the current class instance variable.this can be used to invoke current class method (implicitly)this() can be used
to invoke the current class constructor.this can be passed as an argument in the method call.this can be passed as an argument in the constructor
call.this can be used to return the current class instance from the method.

Q18. Can this keyword be used to refer static members?


Ans) Yes

Q19. Why is Inheritance used in Java?

Runtime polymorphism
code re usability.
data hiding
Method overriding
Q20. Difference between overloading and overriding
Overloading happens when we keep the same method name but change the number or type of parameters. Overriding occurs
when we keep the same method name and signature but change the implementation. Also, we can overload private and static
methods, but we cannot override them.
Q21 Difference between abstract class and interface
Abstract Class Interface

1. An abstract class can contain both abstract and non-abstract Interface contains only abstract methods.
methods.

2. An abstract class can have all four; static, non-static and final, non- Only final and static variables are used.
final variables.

3. To declare abstract class abstract keywords are used. The interface can be declared with the interface keyword.

4 Members are private and protected Members public by default

Q22. Difference between class and interface

Class Interface

The keyword used to create a class is


The keyword used to create an interface is “interface”
“class”

A class can be instantiated i.e., objects


An Interface cannot be instantiated i.e. objects cannot be created.
of a class can be created.

Classes do not support multiple


The interface supports multiple inheritance.
inheritance.

It can be inherited from another class. It cannot inherit a class.

It can be inherited by another class It can be inherited by a class by using the keyword ‘implements’ and it can be
using the keyword ‘extends’. inherited by an interface using the keyword ‘extends’.

It can contain constructors. It cannot contain constructors.

It cannot contain abstract methods. It contains abstract methods only.

Q23. Can I overload the main method ?


Ans yes
Q24 Can we override the main method ?
Ans no
Q25. Can we declare abstract method as final
Ans) No
Q26. Is it necessary to declare a class as abstract if this class contain one abstract method ?
Ans:- Yes
Q27. Can I create an instance of an abstract class

Ans:- Abstract classes cannot be instantiated


Q28. Similarity between abstract class and interface ?

Ans:- they both cannot be implemented, and both contain sets of methods that are defined and must be declared in their
implementation.

Q29. Is it possible to create object of the final class?

Ans:- No, it is not possible to create an object of a final class

Q30 Can final method override?

Ans:- We cannot override the final method in java. If we try to override the final method in java, we get a compilation error

Q31 Difference between abstraction and encapsulation

Ans:- An Abstraction is a process of showing all the required items and protecting the rest.

Encapsulation is the process of binding up data under a single entity.

Q32 Difference between throw and throws?

Ans:- Both of these are concepts used for exception handling, but there is a fundamental difference between throw and throws in
Java. We use the throws keyword to declare what exceptions we can throw from a method. The throw keyword, on the other hand,
is mainly used to throw an exception explicitly within a block of code or a method. We can use the throws keyword in a method
signature. It declares what exceptions a method can throw. Now, the throw keyword can be used in a block of code or within a
method body. It helps in throwing one exception explicitly. These also help in throwing custom exceptions.

Q33:- Difference between exception and error ?

Ans:-

Error Exception
i)An error cannot be handled at runtime. An exception can be handled at runtime
ii)An error can occur both at compile time and during Although all the Exceptions occur at runtime. But checked Exceptions
runtime. can be detected at compile time.
iii)There are 3 types of Errors: Syntax Error, Runtime There are 2 types of Exceptions: Checked Exceptions and Unchecked
Error and Logical Error Exceptions
Q34:-Give Some Example of Exception

Ans:- NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException.

Q34:-What is the name of the Exception class

Ans:- Throwable

Q35:-Difference between Checked and unchecked Exception?

Ans:- Checked exceptions are checked at compile-time.

Unchecked exceptions are not checked at compile-time, but they are checked at runtime.

Q36:-What are try, catch and finally block ?

Ans:-

try The "try" keyword is used to specify a block where we should place an exception code. It means we can't use try block alone.
The try block must be followed by either catch or finally.

catch The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block
alone. It can be followed by finally block later.

finally The "finally" block is used to execute the necessary code of the program. It is executed whether an exception is handled or not.
Q37:-Difference between Final and finally

Final Finally

(1) Once declared, final variable becomes constant and cannot be (1) finally block runs the important code even if exception occurs
modified. or not.
(2) final method cannot be overridden by sub class. (2) finally block cleans up all the resources used in try block
(3) final class cannot be inherited.
Q38:-What is thread ?

Ans: It is a light weight process.


Q39:-Why thread is called light weight process ?

Ans:- they have their own stack but can access shared data. Since threads share the same address space as the process and
other threads within the process, it is easy to communicate between the threads

Q40:-Difference between Thread and process?

Process Thread

A process is an instance of a program that is being executed Thread is a segment of a process or a lightweight process that is
or processed. managed by the scheduler independently.

Processes are independent of each other and hence don't Threads are interdependent and share memory.
share a memory or other resources.

Q41:-What is Concurrency Problem?

Ans:- Threads run at the same time as other parts of the program, there is no way to know in which order the code
will run. When the threads and main program are reading and writing the same variables, the values are
unpredictable. The problems that result from this are called concurrency problems.

Q42:-What is the use of isAlive() function ?

Ans:- isAlive() method of the thread to check whether the thread has finished running before using any
attributes that the thread can change

Q43:-What are the use of following methods for a thread ?

I. Ans:- public void run(): is used to perform action for a thread.

II. public void start(): starts the execution of the thread.JVM calls the run() method on the thread.

III. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for

the specified number of milliseconds.

IV. public void join(): waits for a thread to die.

V. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.

VI. public int getPriority(): returns the priority of the thread.

VII. public int setPriority(int priority): changes the priority of the thread.

VIII. public String getName(): returns the name of the thread.

IX. public void setName(String name): changes the name of the thread.

X. public Thread currentThread(): returns the reference of currently executing thread.

XI. public int getId(): returns the id of the thread.

XII. public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.

XIII. public void suspend(): is used to suspend the thread(depricated).

XIV. public void resume(): is used to resume the suspended thread(depricated).

XV. public void stop(): is used to stop the thread(depricated).

Q44: What are the two ways of implementing thread in Java?

Ans:- Extending the Thread class

Implementing Runnable interface in Java

Q45:- What is the start() and run() method of Thread class?


Ans:- the start() method is used to start or begin the execution of a newly created thread. When the start() method is called, a
new thread is created and this newly created thread executes the task that is kept in the run() method. One can call the start()
method only once.

run(): In simple words, the run() method is used to start or begin the execution of the same thread. When the run() method is
called, no new thread is created as in the case of the start() method. This method is executed by the current thread. One can
call the run() method multiple times

Q46:- What is the synchronization process? Why use it?

Ans:- Synchronization is basically a process in java that enables a simple strategy for avoiding thread interference and memory
consistency errors. This process makes sure that resource will be only used one thread at a time when one thread tries to
access a shared resource.

Q47:-Can you start a thread twice?

Ans:- No, it's not at all possible to restart a thread once a thread gets started and completes its execution. Thread only runs once and
if you try to run it for a second time, then it will throw a runtime exception

Q48:-What is thread priority?

Ans:- Thread priority simply means that threads with the highest priority will get a chance for execution prior to low-
priority threads.

Q49:- Name the method of the thread that is called before the run() method and carries out initialization.

Ans:-start()

Q50:-Name the method that is used to register a thread in a thread scheduler.

. Ans:-start()

Q51:- What is thread priority in Java?

Ans:-Integer

Q52:-Can I write Main() weithout String arg[]

Ans:- No. It will give Runtime error

Q53:What are instance and local variable in Java?

Ans:- Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the
methods and inside the class.

Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The
utilization of the variable is restricted to the block scope.

Q54:-Difference between length() and length ?

Ans:-Length()-to calculate length of the string

Length- to calculate the number of elements of the array

Q55:-What is the use of super keyword in java

 Ans:- Following are the cases when this keyword can be used:
o Accessing data members of parent class when the member names of the class and its child subclasses are same.
o To call the default and parameterized constructor of the parent class inside the child class.
o Accessing the parent class methods when the child classes have overridden them.

Q56:- Can the static methods be overloaded?

Yes! There can be two or more static methods in a class with the same name but differing input parameters.
Q57:-What is JVM

Ans:- JVM - (Java Virtual Machine) JVM is a part of JRE that executes the Java program at the end.

Q58:-What is JDK

Ans:- JDK (Java Development Kit). JDK is the package that contains various tools, Compiler, Java Runtime Environment, etc.

Q59:- What happens if there are multiple main methods inside one class in Java?

Ans:-The program can't compile as the compiler says that the method has been already defined inside the class

Q60 Can you call a constructor of a class inside the another constructor?

Ans:-Yes, the concept can be termed as constructor chaining and can be achieved using this()

Q61: Is it possible to import the same class or package twice in Java and what happens to it during runtime?

Ans:-It is possible to import a class or package more than once, however, it is redundant because the JVM internally loads the package or class
only once.

Q62:-What is WORA in java

Ans:- Java applications are called WORA (Write Once Run Anywhere). This means a programmer can develop Java code on one
system and can expect it to run on any other Java-enabled system without any adjustment

Q63:- What are the features of java

1. Ans:- Simple

2. Object-Oriented

3. Portable

4. Platform independent

5. Secured

6. Robust

7. Architecture neutral

8. Interpreted

9. High Performance

10. Multithreaded

11. Distributed

12. Dynamic

You might also like