Java Viva Questions
Java Viva Questions
3.What is JDK?
JDK stands for Java development kit.
It can compile, document, and package Java programs.
It contains both JRE and development tools.
4.What is JVM?
JVM stands for Java virtual machine.
It is an abstract machine that provides a run-time environment that allows programmers to
execute Java bytecode.
JVM follows specification, implementation, and runtime instance notations.
5.What is JRE?
JRE stands for Java runtime environment.
JRE refers to a runtime environment that allows programmers to execute Java bytecode.
JRE is a physical implementation of the JVM.
6.In Java, what are the differences between heap and stack memory?
Memory
10.What is polymorphism?
Polymorphism is one interface with many implementations. This characteristic allows you to
assign a different meaning or usage to something in different contexts. For example, you can
use polymorphisms to enable more than one form for entities, such as variables, functions, or
objects.
Default Constructor
Does not take any inputs
Main purpose is to initialize the instance variables with the default values
Widely used for object creation
Parameterized Constructor
Capable of initializing the instance variables with the provided values.
These constructors take the arguments.
16.What is a subclass?
A subclass is a class that inherits from another class called the superclass. Subclass can
access all public and protected methods and fields of its superclass.
28.What are Java data types, and how are they grouped?
In Java, a variable must be a specified data type such as an integer, floating-point number,
character Boolean, or string. The two groups of data types are:
Primitive data types, which include byte, short, int, long, float, double, boolean, and char
Non-primitive data types, which include string, arrays, and classes
29.How do you define primitive data types and describe each by size and description?
byte is 1 byte in size. It stores whole numbers from -128 to 127
short is 2 bytes in size. It stores whole numbers from -32,768 to 32,767
int is 4 bytes in size. It stores whole numbers from -2,147,483,648 to 2,147,483,647
long is 8 bytes in size. It stores whole numbers from -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float is 4 bytes in size. It stores fractional numbers and is sufficient for storing 6 to 7 decimal
digits.
double is 8 bytes in size. It stores fractional numbers and is sufficient for storing 15 decimal
digits.
Boolean is 1 bit in size. It stores true or false values.
char is 2 bytes in size. It stores a single character/letter or ASCII values.
35.Can you write multiple catch blocks under a single try block?
Yes, you can have multiple catch blocks under a single try block.
43.What are the differences between unchecked exception, checked exception, and errors?
An Unchecked exception inherits from RuntimeException (which extends from exception). The
JVM treats RuntimeException differently as there is no requirement for the application code to
deal with them explicitly.
A checked exception inherits from the exception class. The client code has to handle the
checked exceptions either in a try-catch clause or has to be thrown for the super-class to catch
the same. A checked exception thrown by a lower class (sub-class) enforces a contract on the
invoking class (super-class) to catch or throw it.
Errors (members of the error family) usually appear for more serious problems, such as
OutOfMemoryError (OOM), that may not be so easy to handle.
// Statements to execute
50.In Java, what are public static void main string args?
Public static void main string args, also known as public static void main(String[] args), means:
Public is an access modifier used to specify who can access this method. Also, this method is
accessible by any class.
main() is made static in Java to access it without creating the instance of a class. If main is not
made static, the compiler will throw an error as main() is called by the JVM before creating any
objects. It can only invoke static methods directly via the class.
Void is the return type of the method that defines the method. That method does not return a
value.
Main is the name of the method searched by JVM as a starting point for an application (with a
particular signature only). It is also the method where the main execution occurs.
String args[] is the parameter that passes to the main method.
51.In Java, what’s the purpose of static methods and static variables?
Developers use a static keyword to make a method or variable shared for all objects when there
is a requirement to share a method or a variable between multiple objects of a class. This is
used instead of creating separate copies for each object.
54.In Java, what are this() and super(), and where are you required to use them?
In Java, super() and this() are special keywords used to call the constructor. When using this()
and super(), they must be the first line of a block.