JAVA_VIVA_QA
JAVA_VIVA_QA
Answer:
o Platform Independent: Java code is compiled into bytecode, which can
be executed on any device with a JVM (Java Virtual Machine).
o Object-Oriented: Java is based on the OOP principles like inheritance,
polymorphism, encapsulation, and abstraction.
o Multithreaded: Java provides built-in support for multithreading, enabling
efficient CPU usage.
o Secure: Java provides a secure environment using the bytecode verifier
and the sandbox model.
o Robust: Java has strong memory management, exception handling, and
type checking, which reduce errors.
o Distributed Computing: Java makes it easier to develop networked and
distributed applications.
Answer: The Java Virtual Machine (JVM) is the engine that runs Java
bytecode. It provides platform independence by converting the bytecode into
machine code specific to the system it is running on. JVM is responsible for
memory management, garbage collection, and providing a runtime environment
for Java programs.
Answer:
o JDK (Java Development Kit): It is a full-featured software development
kit that includes the JRE, compilers, debuggers, and other tools necessary
for Java development.
o JRE (Java Runtime Environment): It provides libraries and the JVM
needed to run Java applications. It does not contain development tools.
o JVM (Java Virtual Machine): It is part of the JRE and is responsible for
executing Java bytecode. JVM makes Java platform-independent.
5. What are the primitive data types in Java?
Answer:
o ==: Compares the memory addresses (references) of two objects. It checks
if two variables point to the same object in memory.
o .equals(): Compares the actual content of two objects. It is defined in the
Object class and can be overridden for custom comparison logic.
Example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
Answer: Polymorphism allows one entity (method, class) to take many forms.
In Java, it is achieved through:
o Method Overloading (Compile-time Polymorphism): Multiple methods
with the same name but different parameters in a class.
o Method Overriding (Runtime Polymorphism): Subclass provides a
specific implementation of a method that is already defined in the
superclass.
Answer:
o String: Immutable. Once a string object is created, its value cannot be
changed.
o StringBuilder: Mutable, but not thread-safe. Suitable for single-threaded
environments.
o StringBuffer: Mutable and thread-safe. Suitable for multi-threaded
environments where string manipulation is needed.
Answer:
o ArrayList: Resizes dynamically and provides fast random access with
constant-time retrieval. It uses a contiguous block of memory.
o LinkedList: A doubly-linked list with slower random access, but provides
faster insertion and removal of elements, especially in the middle.
Answer: The super keyword refers to the superclass of the current object. It is
used to:
o Call a superclass constructor.
o Access superclass methods or fields that are overridden in the subclass.
Example:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
Answer: The java.sql package provides the API for database connectivity and
interaction. It includes classes and interfaces such as Connection, Statement,
PreparedStatement, ResultSet, and DriverManager for working with relational
databases like MySQL, Oracle, and others.
1. url: The connection URL, which specifies the location of the database. It also
includes the type of database (MySQL, PostgreSQL, etc.) and other connection
details (hostname, port number, database name).
2. username: The username used to authenticate with the database.
3. password: The password associated with the username to authenticate and
establish the connection.
JDBC acts as a bridge between Java applications and a database. Here's how it
generally works:
JDBC Components:
How it Works: The Type-4 driver translates JDBC calls directly into the database's
native protocol (such as MySQL's native protocol) and communicates with the
database over the network.
(cj stands for Connector/J, which is the MySQL JDBC driver for Java.).
Simple JAVA MYSQL CONNECTION
import java.sql.*;
try {
conn = DriverManager.getConnection(url, username, password);
System.out.println("Connection successful!");
} catch (SQLException e) {