JAVA Practice Questions
JAVA Practice Questions
Yes
Q4. What are the differences between the constructors and methods?
AnsThe static variable is used to refer to the common property of all objects
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.
Static block is used to initialize the static data member. It is executed before the main method, at the time of class loading.
Ans) No,
Q11. What if the static modifier is removed from the signature of the main method?
Q12. What is the difference between static (class) method and instance method?
Ans) No
Q15. Can we declare the static variables and methods in an abstract class?
Ans)Yes,
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.
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.
Class Interface
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’.
Ans:- they both cannot be implemented, and both contain sets of methods that are defined and must be declared in their
implementation.
Ans:- We cannot override the final method in java. If we try to override the final method in java, we get a compilation error
Ans:- An Abstraction is a process of showing all the required items and protecting the rest.
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.
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:- Throwable
Unchecked exceptions are not checked at compile-time, but they are checked at runtime.
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:- 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
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.
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.
Ans:- isAlive() method of the thread to check whether the thread has finished running before using any
attributes that the thread can change
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
V. public void join(long miliseconds): waits for a thread to die for the specified miliseconds.
VII. public int setPriority(int priority): changes the priority of the thread.
IX. public void setName(String name): changes the name of the thread.
XII. public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
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
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.
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
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()
. Ans:-start()
Ans:-Integer
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.
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.
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.
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
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