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

Java Viva

The document provides a comprehensive overview of Java programming basics, including definitions of key concepts like Java, JDK, JRE, JVM, classes, objects, and primitive data types. It also covers control structures, methods, type casting, packages, and OOP principles such as encapsulation, inheritance, polymorphism, and abstraction. Additionally, it explains the significance of keywords like static, final, and void, along with the use of constructors and the super keyword.

Uploaded by

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

Java Viva

The document provides a comprehensive overview of Java programming basics, including definitions of key concepts like Java, JDK, JRE, JVM, classes, objects, and primitive data types. It also covers control structures, methods, type casting, packages, and OOP principles such as encapsulation, inheritance, polymorphism, and abstraction. Additionally, it explains the significance of keywords like static, final, and void, along with the use of constructors and the super keyword.

Uploaded by

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

Java Basics Questions:

1. What is Java?

o Answer: Java is a high-level, object-oriented programming language developed by


Sun Microsystems (now owned by Oracle). It is platform-independent, meaning that
Java programs can run on any device with a Java Virtual Machine (JVM). This is often
summarized by the phrase "Write Once, Run Anywhere."

2. Explain the difference between JDK, JRE, and JVM.

o Answer:

 JDK (Java Development Kit): A full software development kit that includes
the JRE and development tools such as the compiler (javac), debugger, and
documentation generator.

 JRE (Java Runtime Environment): Provides the libraries, Java Virtual


Machine (JVM), and other components required to run Java applications. It
does not include development tools.

 JVM (Java Virtual Machine): A virtual machine that runs Java bytecode and
allows Java programs to be platform-independent by abstracting the
underlying OS.

3. What is the main difference between a class and an object in Java?

o Answer: A class is a blueprint or template for creating objects, whereas an object is


an instance of a class. A class defines properties (fields) and behaviors (methods),
while an object is a real-world entity that has those properties and can perform
those behaviors.

4. What are primitive data types in Java? Can you name them?

o Answer: Primitive data types are basic types that are built into Java. They include:

 byte, short, int, long (for integer values)

 float, double (for floating-point values)

 char (for single characters)

 boolean (for true/false values)

5. What are control structures in Java? Can you give examples of loops and conditionals?

o Answer: Control structures in Java determine the flow of execution based on


conditions.

 Conditionals: if, else if, else, switch

 Loops: for, while, do-while

6. What is the purpose of the static keyword in Java?

o Answer: The static keyword is used to declare class-level variables and methods that
are shared by all instances of a class. It means that the member belongs to the class
rather than an instance of the class. For example, static int count; means count is
shared across all objects of the class.

7. What is a method in Java? How do you define and call a method?

o Answer: A method is a block of code that performs a specific task. It is defined with a
return type, name, and parameters.

 Example of method definition:

 public void greet() {

 System.out.println("Hello!");

 }

 Method call:

 greet(); // Calling the method

8. What is the purpose of the main method in Java?

o Answer: The main method is the entry point for any Java application. It is where the
program starts execution. It must be defined as public static void main(String[] args).

9. Explain the concept of type casting in Java. What is the difference between implicit and
explicit type casting?

o Answer: Type casting is the process of converting one data type to another.

 Implicit Casting (Widening): Happens automatically when converting smaller


types to larger types, like int to long.

 Explicit Casting (Narrowing): Requires manual intervention when converting


larger types to smaller types, like double to int.

10. What is a package in Java? Why are packages used in Java?

o Answer: A package is a namespace that organizes classes and interfaces in Java. It


helps avoid name conflicts, organizes code, and controls access using access
modifiers. For example, java.util is a package that contains utility classes like
ArrayList.

11. What is the difference between == and equals() method in Java?

o Answer:

 == compares the references of two objects (i.e., whether they point to the
same memory location).

 equals() is used to compare the content of two objects. It should be


overridden in custom classes to define what content equality means.

12. What is the purpose of the final keyword in Java?

o Answer: The final keyword can be used for:

 Variables: A constant whose value cannot be changed.


 Methods: A method that cannot be overridden.

 Classes: A class that cannot be subclassed.

13. What is a constructor in Java? How does it differ from a method?

o Answer: A constructor is a special type of method used to initialize objects. It is


invoked when an object is created and has the same name as the class. Constructors
do not have a return type.

 Example:

 public class Car {

 public Car() {

 System.out.println("Car created!");

 }

 }

14. What are arrays in Java? How do you declare and initialize an array?

o Answer: An array in Java is a collection of elements of the same type. It can be


declared as:

o int[] numbers = new int[5]; // Declaring an array

o numbers[0] = 10; // Initializing elements

Or initialized directly:

int[] numbers = {10, 20, 30, 40, 50};

15. What is the significance of the void keyword in Java methods?

o Answer: void indicates that the method does not return any value. If a method is
defined with void, it performs an action but does not return anything to the caller.
Object-Oriented Programming (OOP) Concepts in Java:

1. What is Object-Oriented Programming (OOP)? Can you list and explain its main principles?

o Answer: OOP is a programming paradigm based on the concept of "objects," which


can contain data and methods. Its main principles are:

 Encapsulation: Bundling data and methods together and restricting access to


certain components (using access modifiers).

 Abstraction: Hiding complex implementation details and showing only


essential features (e.g., using abstract classes or interfaces).

 Inheritance: A mechanism to create a new class by inheriting attributes and


behaviors from an existing class.

 Polymorphism: The ability of objects to take on multiple forms, either


through method overloading or overriding.

2. What is inheritance in Java? How is it implemented?

o Answer: Inheritance allows a new class to acquire properties and methods from an
existing class. It is implemented using the extends keyword.

o class Animal {

o void sound() { System.out.println("Animal makes a sound"); }

o }

o class Dog extends Animal {

o void sound() { System.out.println("Dog barks"); }

o }

3. Can you explain the difference between extends and implements in Java?

o Answer:

 extends is used to inherit from a class (single inheritance).

 implements is used to implement an interface (a class can implement


multiple interfaces).

4. What is polymorphism in Java? Explain with examples.

o Answer: Polymorphism allows objects to behave in different ways based on their


actual class type. It can be achieved through method overloading and overriding.

 Example:

 class Animal {

 void sound() { System.out.println("Animal makes a sound"); }


 }

 class Dog extends Animal {

 void sound() { System.out.println("Dog barks"); }

 }

 Animal myAnimal = new Dog();

 myAnimal.sound(); // Outputs: Dog barks

5. What is method overloading and method overriding in Java? How do they differ?

o Answer:

 Method Overloading: Defining multiple methods with the same name but
different parameters.

 Method Overriding: Defining a method in a subclass with the same


signature as the method in the superclass to provide a specific
implementation.

6. What is encapsulation in Java? How do you implement encapsulation in Java?

o Answer: Encapsulation is the concept of restricting access to certain details of an


object's implementation. It is implemented by using private fields and public
getter/setter methods.

o class Employee {

o private int salary;

o public int getSalary() {

o return salary;

o }

o public void setSalary(int salary) {

o this.salary = salary;

o }

o }

7. What is abstraction in Java? How is abstraction implemented?


o Answer: Abstraction is the concept of hiding the implementation details and
showing only the essential features. It can be implemented using abstract classes or
interfaces.

o abstract class Animal {

o abstract void sound();

o }

8. Can you explain the concept of "super" keyword in Java and give an example of its use?

o Answer: The super keyword refers to the superclass (parent class). It is used to call
the parent class's constructor or methods.

o class Animal {

o Animal() {

o System.out.println("Animal Constructor");

o }

o }

o class Dog extends Animal {

o Dog() {

o super(); // Calls the parent class constructor

o System.out.println("Dog Constructor");

o }

o }

You might also like