Java Questions
Java Questions
ANSWERS
Here are the answers to your Java questions:
- **Encapsulation**: Wrapping data (variables) and methods (functions) into a single unit, known as a
class, and restricting direct access to some of the object’s components. Example: Using private variables
and public getter/setter methods.
- **Inheritance**: Mechanism by which one class acquires properties (fields and methods) of another.
This allows for code reusability. Example: `class Dog extends Animal`.
- **Polymorphism**: The ability to perform a single action in different ways. There are two types:
- **Abstraction**: Hiding the implementation details and showing only the functionality. Achieved using
abstract classes and interfaces.
```java
import java.util.Scanner;
scanner.close();
}
```
- **JDK (Java Development Kit)**: A software development environment used for developing Java
applications. It includes JRE, development tools like a compiler (`javac`), and libraries for development.
- **JRE (Java Runtime Environment)**: It provides libraries and other components to run applications
written in Java. It includes the JVM but does not include development tools like the compiler.
- **JVM (Java Virtual Machine)**: The engine that runs Java bytecode. It converts the compiled Java
code into machine code to be executed by the machine. JVM is platform-dependent but Java is
platform-independent because of JVM.
1. Java code is written and compiled into bytecode (.class file) by JDK.
A **constructor** is a special method used to initialize objects. It is called when an object of a class is
created. A constructor differs from a method in these ways:
- A constructor has the same name as the class and no return type (not even `void`).
Example:
```java
class Dog {
String name;
Dog(String name) {
this.name = name;
}
}
```
```java
```
```java
```
```java
// Code here
```
### 6. Java Program to Print Numbers 1 to 100, Skipping Numbers Divisible by Both 3 and 4
```java
if (i % 3 == 0 && i % 4 == 0) {
continue; // Skip numbers divisible by both 3 and 4
System.out.println(i);
```
- **ArrayList**: Internally backed by an array. It offers fast random access but slow insertions and
deletions (especially in the middle of the list) because elements need to be shifted.
- **LinkedList**: Implemented as a doubly linked list. It provides faster insertions and deletions but
slower random access (because each element must be traversed sequentially).
**Use Cases**:
- Use `ArrayList` when you need frequent access to elements via index.
```java
import java.util.Scanner;
int n = scanner.nextInt();
arr[i] = scanner.nextInt();
// Bubble Sort
arr[j + 1] = temp;
System.out.println("Sorted list:");
scanner.close();
```
- **continue**: Skips the current iteration and moves to the next iteration.
Example:
```java
if (i == 5) {
System.out.println(i);
if (i == 5) {
System.out.println(i);
```
```java
import java.util.Scanner;
double result;
switch (operator) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
if (num2 != 0) {
} else {
return;
}
break;
default:
System.out.println("Invalid operator.");
return;
scanner.close();
```