0% found this document useful (0 votes)
22 views5 pages

Java Viva Questions and Answers Guide

The document contains a series of Java viva questions covering various topics including arrays, classes, stacks, method overloading, nested classes, method overriding, abstract classes, interfaces, packages, exceptions, and multithreading. Each section provides definitions, syntax, and explanations of concepts such as command line arguments, object-oriented principles, and thread management. It serves as a comprehensive guide for understanding fundamental Java programming concepts.

Uploaded by

Amogh B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Java Viva Questions and Answers Guide

The document contains a series of Java viva questions covering various topics including arrays, classes, stacks, method overloading, nested classes, method overriding, abstract classes, interfaces, packages, exceptions, and multithreading. Each section provides definitions, syntax, and explanations of concepts such as command line arguments, object-oriented principles, and thread management. It serves as a comprehensive guide for understanding fundamental Java programming concepts.

Uploaded by

Amogh B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA VIVA QUESTIONS

PROGRAM 01 (Arrays)

1. What is command line argument?


Command line arguments are one way of giving input to program. It is given at command
prompt while executing the program.

2. What is the data type of command-line arguments in Java?


String

3. What is an array?
Array is group of values of same data type stored in consecutive memory locations.

4. Give syntax of declaring 1D array.


<data type>[] <array name> = new <data type>[ARRAY SIZE];

5. Give syntax of declaring 2D array.


<data type>[][] <array name> = new <data type>[ROW SIZE][COLUMN SIZE];

6. What is the default value of array elements in Java?


int is 0 and String is null.

7. Can arrays hold primitive types and objects?


Yes, array can hold both primitive types and objects.

8. How do you find the length of an array?


Use the .length property (not a method, so no parentheses).

9. What happens if you access an index out of bounds?


An exception by name ArrayIndexOutOfBounds occurs.

PROGRAM 02 (Class)

1. What is an object?
An object in Java is a real-world entity created from a class. It represents an instance of
a class.

2. What is a class?
Class is group of objects with similar properties and behavior. A class in Java is a
blueprint for creating objects, grouping related variables (fields) and methods (functions)
into a single unit.

3. What is constructor?
Constructor is a method in java which is automatically called when an object is created. It
has same name as class name. It is used to initialize data members of an object of a class.
4. What are different types of constructor?
Default constructor (constructor with arguments)
parameterized constructor (constructor with arguments)
copy constructor (constructor with object of same class as argument)

PROGRAM 03 (Stack)

1. What is stack?
Stack is a data structure in which items added first is deleted last. Its FILO data
structure.

2. What happens when an item is added to stack which is full?


Overflow error occurs when stack is full.

3. What happens when an item is deleted with stack is empty?


Underflow error occurs when stack is empty.

4. What is the difference between peek and pop?


Pop returns topmost value in stack and deletes it from stack. Peek returns topmost
value in stack and DOES NOT delete it from stack.

5. What is significance of final keyword when applied to a data member?


Value of a final data member cannot be changed in the program.

PROGRAM 04 (Method Overloading)

1. What is method overloading?


Method overloading is a concept in object-oriented programming (OOP) where
multiple methods in the same class share the same name but differ in their
parameter lists (number of parameters, types of parameters, or both).

2. When is toString() method called?


When an object is passed to [Link]() or [Link](), Java
automatically calls toString() to get the string representation.

3. What is compile time polymorphism?


Compile-time polymorphism is a type of polymorphism in which the method call
is resolved at compile time rather than at runtime.

4. What happens when exact matching method is not found in method overloading?
If no exact match exists, the compiler tries widening conversions.
Example : calling method(10) may match method(long) if method(int) doesn’t
exist.

5. Define ‘this’ keyword in java.


The this keyword in Java is a special reference variable that refers to the current
object or invoking object.
PROGRAM 5 (Nested Class)

1. What is a nested class in Java? How is it different from a regular class?


A nested class is a class defined within another class. It helps logically group classes that
are only used in one place. Unlike regular classes, nested classes can access members of
the outer class, including private ones (if non-static).

2. How do you instantiate a non-static inner class from outside the outer class?
First create an instance of the outer class,
Outer outerObj = new Outer();
then use it to create the inner class:
[Link] obj = [Link] Inner();

3. Can the inner class access private members of the outer class?
Yes, a non-static inner class can access all members of the outer class, including private
ones.

PROGRAM 6 (Method Overriding)

1. What is method overriding?


Method Overriding allows parent class and child class to have methods with same name.
When child class object calls overridden method, child class method overrides parent
class method.

2. What is @Override?
@Override is annotation statement which indicates following method overrides a method
in superclass.

3. What is runtime polymorphism or dynamic dispatch?


Whenever parent class and child class have methods with same name, resolving call to
that method at runtime rather than at compile time is called dynamic dispatch or runtime
polymorphism.

4. What is final method?


A method which cannot be overridden by subclass is called final method.

PROGRAM 7 (Abstract Class)

1. What is abstract class?


An abstract class in Java is a class declared with the abstract keyword that cannot be
instantiated directly.

2. What is abstract method?


An abstract method in Java is a method that is declared without an implementation (no
body). It is meant to be overridden in a subclass.
PROGRAM 8 (Interface)

1. What is interface?

An interface in Java is a contract that defines a set of methods a class must implement,
without specifying how those methods should be carried out. It is used to achieve
abstraction and multiple inheritance in Java.

2. What type of data members and methods can an interface have ?

Interface can have


static and final data members.
static, private and default methods.
abstract methods

Interface cannot have


non static data members and non static public methods

PROGRAM 9 (Package)

1. What is a package?
Package is the folder that contains classes, abstract classes and interfaces.

2. What is the purpose of creating packages?


Packages provide a unique namespace, preventing clashes between classes with the same
name.
Example: You can have pkg.subpkg1.A and pkg.subpkg1.A without conflict.

3. What is the default package in Java?


[Link].*;

PROGRAM 10 (Exception)
1. What is an exception?
An exception is an error that occurs during the execution of program and cause program
to crash.

2. What is custom exception?


Custom exception is an exception that is defined by user and is created by extending
Exception and RuntimeException classes in Java.

3. What is chained exception?


Chained exception is associating one exception with another exception where second
exception is cause of another exception.

4. Give syntax of throw statement?


throw statement is used to explicitly raise a built in exception or custom exception.
Syntax :
throw <object of Exception>;
5. List any three built in exceptions.
Built-in Exceptions →
NullPointerException
ArrayIndexOutOfBoundsException
ArithmeticException.

PROGRAM 11 (Multithreading), PROGRAM 12 (Multithreading)

1. What is multithreading?
Multithreading is the concurrent execution of two or more threads in a program.

2. What is synchronization?
Synchronization ensures that only one thread accesses a shared resource at a time.

3. What happens when sleep method of Thread class is called?


The sleep() method pauses the current thread for a specified time.

4. Which method is automatically called when a thread is started?


The run() method is automatically called when a thread is started with start().

5. How is thread created in Java?


A thread is created by extending Thread class or implementing Runnable interface.

6. What are different stages in which thread exists during its life cycle?
NEW, RUNNABLE, TIME_WAITING, WAITING, BLOCKED and TERMINATED.

7. State the purpose of join() method of Thread class?


The join() method makes calling thread complete its execution before main thread
continues its execution.

You might also like