0% found this document useful (0 votes)
36 views4 pages

Java Practical Viva Questions Answers

java

Uploaded by

tejasjagdale50
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)
36 views4 pages

Java Practical Viva Questions Answers

java

Uploaded by

tejasjagdale50
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
You are on page 1/ 4

Java Practical Viva Questions and Answers

1. Control Structures
Control structures are used to dictate the flow of execution in a program. They help in
making decisions, repeating actions, or branching the control flow.

The difference is that 'if' statements check conditions, 'switch' executes code based on the
value of a variable, and loops repeat actions.

A nested loop is a loop inside another loop. For example:

for (int i = 0; i < 3; i++) {


for (int j = 0; j < 2; j++) {
System.out.println(i + j);
}
}

Example of an if-else statement:

int number = 10;


if (number > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
}

2. Class and Object Creation


A class is a blueprint for creating objects, while an object is an instance of a class.

Constructors are special methods that are called when an object is created. They often
initialize the object’s attributes.

You create an object using the following syntax:

ClassName obj = new ClassName();

The 'this' keyword refers to the current object. It helps to differentiate between class
attributes and parameters.

3. Method Overloading
Method overloading allows multiple methods to have the same name but different
parameters.
Example of method overloading:

void add(int a, int b) {}


void add(double a, double b) {}

Java determines which method to call based on the method signature (method name and
parameter types).

Benefits include code readability and reusability.

4. Single Inheritance
Inheritance allows a class to inherit properties and methods from another class. Single
inheritance means a class inherits from one superclass.

You create a subclass using the 'extends' keyword:

class Dog extends Animal {}

Example of single inheritance:

class Animal {}
class Dog extends Animal {}

The 'super' keyword is used to refer to the superclass's methods and constructors.

5. Polymorphism
Polymorphism allows methods to do different things based on the object that it is acting
upon.

Compile-time polymorphism is method overloading, while runtime polymorphism is


method overriding.

Example: Method Overloading (compile-time):


void display(int a) {}
void display(String b) {}

Method Overriding (runtime):


class Animal { void sound() {} }
class Dog extends Animal { void sound() {} }

Polymorphism is important for flexibility and maintaining code.

6. Arrays
An array is a collection of elements of the same type, while an ArrayList can grow and
shrink in size.

Declare an array: int[] numbers = {1, 2, 3};


Example of iterating through an array:

for (int i = 0; i < numbers.length; i++) {


System.out.println(numbers[i]);
}

Common operations include sorting, searching, and updating elements.

7. Packages
Packages are used to group related classes and interfaces. They help in organizing code.

You create a package using the 'package' keyword at the top of your Java file:

package myPackage;

To import a package, use 'import packageName.ClassName;' at the beginning of your code.

Benefits include avoiding naming conflicts and better organization.

8. Interfaces
An interface is a reference type in Java that can contain only constants, method signatures,
default methods, static methods, and nested types. It is different from an abstract class
because it cannot have instance fields.

You implement an interface in a class using the 'implements' keyword:

class MyClass implements MyInterface {}

Example:

interface Animal { void sound(); }


class Dog implements Animal { void sound() {} }

Advantages include multiple inheritance and defining contracts for classes.

9. Exception Handling
Exception handling in Java is a mechanism to handle runtime errors, allowing the normal
flow of the program to be maintained.

Checked exceptions must be declared or handled, while unchecked exceptions do not


require handling.

Syntax:

try {
// code that may throw an exception
} catch (ExceptionType e) {
// handle exception
} finally {
// cleanup code
}

Example:

try {
int result = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero");
}

You might also like