Java QB Answers
Java QB Answers
Bytecode in Java is an intermediate representation of a Java program. When Java source code is
compiled, it is not converted directly to machine code. Instead, it is compiled into bytecode by the
Java compiler. Bytecode is a set of instructions that can be executed by the Java Virtual Machine
(JVM).
Advantages of Bytecode:
Example:
System.out.println("Hello, World!");
1. Platform Independence: Write once, run anywhere due to the use of bytecode and JVM.
3. Robust: Features like garbage collection and exception handling make Java reliable.
5. Secure: Includes a runtime environment with security features like bytecode verification.
Condition Condition is checked before executing the Condition is checked after executing the
Check loop. loop.
Example:
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
int j = 1;
do {
System.out.println(j);
j++;
2. class Example {
3. Example() {
4. System.out.println("Default Constructor");
5. }
6. }
8. class Example {
9. Example(int x) {
11. }
12. }
13. Copy Constructor: Initializes an object by copying data from another object.
15. int x;
16. Example(Example e) {
18. }
19. }
5. Commonly Used Methods of the File Class
3. file.createNewFile();
6. file.delete();
9. dir.mkdir();
13. oldFile.renameTo(newFile);
import java.util.Arrays;
import java.util.Scanner;
int n = scanner.nextInt();
System.out.println("Enter elements:");
arr[i] = scanner.nextInt();
Arrays.sort(arr);
System.out.println("Sorted Array: " + Arrays.toString(arr));
Inheritance allows one class (child) to acquire the properties and methods of another class (parent).
Example:
class Parent {
void display() {
void show() {
Thread Lifecycle:
• New
• Runnable
• Running
• Blocked
• Terminated
System.out.println("Thread is running.");
thread.start();
Example:
System.out.println(arr[0]);
import java.io.IOException;
try {
// Creating a file
if (file.createNewFile()) {
System.out.println("File created");
// Deleting a file
if (deleteFile.delete()) {
System.out.println("File deleted");
// Renaming a file
if (oldFile.renameTo(newFile)) {
System.out.println("File renamed");
// Creating a directory
if (directory.mkdir()) {
System.out.println("Directory created");
} catch (IOException e) {
}
14. Control Structures: if-else and switch
if-else Example:
if (x > 0) {
System.out.println("Positive");
} else {
System.out.println("Negative");
switch Example:
switch (day) {
default: System.out.println("Other");
Example:
class Example {
int x;
Example(int x) {
this.x = x;