Important Question Answers
Important Question Answers
Explanation:
Q2. What is the use of this keyword? How is it different from super keyword?
this Keyword:
Example:
class Student {
int id;
Student(int id) {
this.id = id; // Refers to current object
}
}
super Keyword:
Example:
class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void display() {
System.out.println(this.x); // Child’s x
System.out.println(super.x); // Parent’s x
}
}
Keyword Refers To Usage
this Current class Access variables, methods, constructors
super Parent (super) class Access overridden or hidden members
Q3. What is Java Bytecode? What is its extension? Which software is needed
to run it?
Java Bytecode:
.class
JVM (Java Virtual Machine) – part of the Java Runtime Environment (JRE).
✅ Example:
Q4. Program demonstrating import, new, this, break, continue. How to compile
and run.
import java.util.Scanner; // import
class Demo {
int num;
Demo(int num) {
this.num = num; // this
}
void show() {
for (int i = 1; i <= num; i++) {
if (i == 3) continue; // continue
if (i == 6) break; // break
System.out.println("i = " + i);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();
Given:
class A {}
class B extends A {}
Now, evaluate:
Code:
java
Copy code
public class EvaluateExpression {
public static void main(String[] args) {
if (args.length != 3) {
System.out.println("Usage: java EvaluateExpression num1
operator num2");
return;
}
double result = 0;
double num1 = Double.parseDouble(args[0]);
char operator = args[1].charAt(0);
double num2 = Double.parseDouble(args[2]);
switch (operator) {
case '+': result = num1 + num2; break;
case '-': result = num1 - num2; break;
case '*': result = num1 * num2; break;
case '/': result = num2 != 0 ? num1 / num2 : Double.NaN; break;
default: System.out.println("Invalid operator"); return;
}
How to Run:
bash
Copy code
javac EvaluateExpression.java
java EvaluateExpression 10 + 5
JavaFX provides layout panes to manage the position and size of UI elements.
Features of Java:
1. Platform Independent
2. Object-Oriented
3. Simple
4. Secure
5. Robust
6. Multithreaded
7. High Performance
8. Dynamic and Extensible
Explaining Two:
1. Platform Independent:
o Java code is compiled into bytecode (.class file) which can run on any system with a
JVM.
o "Write Once, Run Anywhere"
2. Object-Oriented:
o Everything in Java is treated as an object.
o It supports concepts like inheritance, encapsulation, abstraction, and polymorphism.
1. JavaFX API: The topmost layer providing GUI components like shapes, charts, controls.
2. Quantum Toolkit: Acts as a bridge between JavaFX API and the operating system.
3. Prism: Graphics pipeline that uses DirectX/OpenGL for rendering.
4. Glass Windowing Toolkit: Handles native windowing, input, and events.
5. Java Runtime: Underlying Java Virtual Machine (JVM).
Access Specifiers:
Specifier Scope
Modifiers:
✅ Example:
java
Copy code
public class MyClass {
private static final int VALUE = 10;
}
1. Single Inheritance:
o One subclass inherits from one superclass.
java
Copy code
class A {}
class B extends A {}
2. Multilevel Inheritance:
o Chain of inheritance.
java
Copy code
class A {}
class B extends A {}
class C extends B {}
3. Hierarchical Inheritance:
o Multiple classes inherit from one base class.
java
Copy code
class A {}
class B extends A {}
class C extends A {}
✅ Note: Java does not support multiple inheritance using classes (to avoid ambiguity), but interfaces
can be used.
Default Constructor:
No arguments.
java
Copy code
class A {
A() {
System.out.println("Default Constructor");
}
}
Parameterized Constructor:
With arguments.
java
Copy code
class A {
A(int x) {
System.out.println("x = " + x);
}
}
Shallow Copy:
Copies references.
java
Copy code
A a1 = new A();
A a2 = a1; // Both point to same object
Deep Copy:
java
Copy code
A a2 = new A(a1); // Copy constructor
Interface:
A blueprint for a class with abstract methods.
java
Copy code
interface Animal {
void sound();
}
java
Copy code
int a = ; // Missing value
2. Runtime Error:
o Occur during execution.
java
Copy code
int a = 10 / 0; // ArithmeticException
3. Logic Error:
o Produces wrong output.
java
Copy code
System.out.println("Sum = " + (a - b)); // Should be a + b
Example:
java
Copy code
String s = "Hello";
s.concat("World"); // s remains "Hello"
throw:
java
Copy code
throw new ArithmeticException("Divide by Zero");
throws:
java
Copy code
void check() throws IOException {
// code that might throw exception
}
Example Program:
java
Copy code
class Example {
static void validate(int age) throws ArithmeticException {
if (age < 18)
throw new ArithmeticException("Not eligible");
}
public StopWatch() {
startTime = System.currentTimeMillis();
}
Handling
Must handle using try-catch Optional
Required?
IOException, NullPointerException,
Examples
SQLException ArithmeticException
Example:
java
Copy code
// Checked
FileReader fr = new FileReader("file.txt"); // Must handle
// Unchecked
int x = 10 / 0; // ArithmeticException
primaryStage.setTitle("Red Circle");
primaryStage.setScene(scene);
primaryStage.show();
}
1. Default Constructor:
o No arguments.
java
Copy code
class A {
A() { System.out.println("Default"); }
}
2. Parameterized Constructor:
o Takes arguments.
java
Copy code
A(int x) { this.x = x; }
3. Copy Constructor:
o Copies data from another object.
java
Copy code
A(A obj) { this.x = obj.x; }
✅ Java does not provide a built-in copy constructor, but you can define it manually.
java
Copy code
class A {
void display() {}
}
class B extends A {
void display() {} // overridden
}
Types:
Single
Multilevel
Hierarchical
Example:
java
Copy code
class A {
void show() { System.out.println("A"); }
}
class B extends A {
void display() { System.out.println("B"); }
}
java
Copy code
final class A {} // class B extends A → Error
java
Copy code
class A {
final void display() {}
}
Creating Scene:
java
Copy code
Scene scene = new Scene(root, width, height);
java
Copy code
stage.setScene(scene);
stage.show();
java
Copy code
stage.setScene(scene2);
✅ You can use multiple scenes for different views like login → dashboard.
Methods All are abstract by default Can have both concrete and abstract methods
java
Copy code
abstract class Animal {
abstract void sound();
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}
Mouse Events:
java
Copy code
circle.setOnMouseClicked(e -> {
System.out.println("Mouse Clicked");
});
Key Events:
java
Copy code
scene.setOnKeyPressed(e -> {
System.out.println("Key Pressed: " + e.getCode());
});