Java Answers
Java Answers
Ans:
System.out.println(arg);
2. What is a Thread?
A thread is a lightweight process that can run concurrently with other threads. Threads can be
created in two ways:
Ans:
System.out.println("Thread is running");
t.start();
Advantages of Multithreading:
- Improved performance
- Resource sharing
Ans:
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Arithmetic Exception");
} finally {
5. Unicode in Java
Ans:
Unicode is a character encoding standard that allows Java to support internationalization. It enables
the representation of characters from multiple languages.
Ans:
Example:
void sound() {
System.out.println("Bark");
}
7. Print Pattern
Ans:
import java.util.Scanner;
int n = sc.nextInt();
System.out.println();
8. Constructor Overloading
Ans:
Constructor overloading allows a class to have more than one constructor with different parameters.
class Box {
Box() {
System.out.println("Default constructor");
Box(int length) {
9. Super Keyword
Ans: The `super` keyword is used to refer to the immediate parent class. Its purposes include:
Ans:
import java.util.Arrays;
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
Ans:
Garbage collection is the process of automatically freeing memory by removing objects that are no
longer in use. The `finalize()` method is called by the garbage collector before an object is removed.
Refer to answer 2.
Ans:
Java does not support multiple inheritance directly but allows it through interfaces.
interface A {
void methodA();
interface B {
void methodB();
class C implements A, B {
System.out.println("Method A");
System.out.println("Method B"); }
}
14. Explain the each word in the statement public static void main (String args[])
Ans:
Ans:
- Abstract Class: Can have method implementations, can have state (fields).
- Interface: Cannot have method implementations (until Java 8), only method signatures.
Ans:
import java.util.Scanner;
int n = sc.nextInt();
System.out.println();
}
17. Constructor and Its Role
Ans:
class Car {
Car() {
System.out.println("Car created");
Ans:
Ans:
class A {
void display() {
System.out.println("Class A");
class B extends A {
void display() {
System.out.println("Class B");
class C extends A {
void display() {
System.out.println("Class C");
Ans:
class Box {
int length;
Box(Box b) {
this.length = b.length;
Ans:
- Declare constants
- Prevent inheritance
22. Base Class Constructor
Ans:
class A {
A() {
System.out.println("Class A Constructor");
class B extends A {
B() {
System.out.println("Class B Constructor");
class C extends B {
C() {
System.out.println("Class C Constructor");
}
23. Summation Series
Ans:
import java.util.Scanner;
System.out.print("Enter n: ");
int n = sc.nextInt();
double sum = 0;
Ans:
class Encapsulated {
this.data = data;
return data;
}
25. Static Data Member
Ans:
Ans:
class A {
class B extends A {
class C extends B {
Ans:
try {
int a = 10 / 0;
String s = null;
s.length();
System.out.println(e);
}
Access Specifiers: Control the visibility of class members.
Ans:
super(message);
try {
} catch (MyException e) {
System.out.println(e.getMessage());
Ans:
class Box {
length = l;
breadth = b;
height = h;
int volume() {
Ans:
Java is called a platform-independent language because of its ability to "Write Once, Run Anywhere"
(WORA). This means that Java code compiled on one system can run on any other system with a Java
Virtual Machine (JVM) installed, regardless of the underlying operating system or hardware
architecture.
1. Compilation: When you compile Java source code, it's not directly translated into machine
code specific to a particular operating system. Instead, it's compiled into bytecode.
3. JVM: The JVM is a software layer that sits between the bytecode and the operating system.
It acts as an interpreter for the bytecode, translating it into machine code that the specific
operating system can understand and execute.
31. Generic Catch Block
Ans:
Justification:
• Order of Execution: In a try-catch block, the catch blocks are executed in the order they
appear.
• Specificity: Specific catch blocks (e.g., catch(IOException e)) handle specific types of
exceptions.
• Generic Catch Block: The generic catch(Exception e) block catches any exception that occurs
within the try block.
1. Specificity: If a specific catch block can handle an exception, it will be executed before the
generic catch block. This ensures that the most specific and relevant handling logic is applied.
2. Avoiding Incorrect Handling: Placing the generic catch block first would cause it to catch all
exceptions, even those that could be handled more specifically by other catch blocks. This
might lead to improper or incomplete handling of the exception.
Ans:
- Mutable: Can be changed (e.g., StringBuilder).
Ans:
import java.util.Scanner;
int n = sc.nextInt();
int fact = 1;
fact *= i;
Ans:
class Parent {
Ans:
class Test {
try {
t.method();
} catch (Exception e) {
System.out.println(e.getMessage());
}
36. Access Specifiers (Repeated)
Ans:
Rectangle(double l, double w) {
length = l;
width = w;
double area() {
Triangle(double b, double h) {
base = b;
height = h;
double area() {
}
public class ShapeTest {
Ans:
Ans:
if (i % j == 0) {
isPrime = false;
break;
if (isPrime) {
}
}
Ans:
Ans:
class MyClass {
int value;
MyClass(int value) {
this.value = value;
modify(obj);
System.out.println(obj.value); // Output: 20
obj.value = 20;
Ans:
• Reason: Interface members are meant to define a contract that is accessible to all
implementing classes. Making them public ensures that they are universally visible to any
class implementing the interface.
2. Static
• Reason: Interface fields are constants and do not belong to any specific instance. They are
associated with the interface itself, rather than with objects of the implementing classes.
• Implication: This ensures that the variable can be accessed without creating an instance of
the interface.
3. Final
• Reason: Interface fields are constants by design. They are meant to represent fixed values
that cannot be changed. The final modifier ensures that the value of the variable cannot be
modified after initialization.
• Implication: This enforces immutability for interface fields, ensuring consistency across all
uses.
Ans:
• Reason: Methods in an interface define a contract or blueprint that any implementing class
must follow. Declaring them as abstract (implicitly) means that they do not have a method
body in the interface itself and must be implemented by the classes that implement the
interface.
• Implication:
o It ensures that the implementing classes provide their own implementation for the
method.
o Starting from Java 8, interfaces can also have default methods (with method bodies)
and static methods, which are exceptions to the "abstract by default" rule.
• Reason: Declaring interface methods as final would contradict the purpose of an interface.
An interface is designed to allow implementing classes to provide their own implementation
of the methods. The final modifier, on the other hand, prevents overriding, which would
make the interface unusable as a contract for implementation.
• Why Final Is Not Allowed:
o If a method in an interface were declared final, no class could provide its own
implementation for that method, defeating the purpose of the interface itself.
Ans:
Ans:
Abstraction
Encapsulation
Key Differences
• Focus: Abstraction focuses on what the object does, while encapsulation focuses on how the
object's data is accessed and manipulated.10
In essence:
While they are distinct concepts, they often work together. Encapsulation can be used to implement
abstraction by hiding the internal details of a class and providing a controlled interface for accessing
its functionality.16
48. “In a two-dimensional array in JAVA, the length of every 1D array may differ”- justify the
statement with a program.
Ans:
In Java, a two-dimensional array can indeed consist of rows that vary in length, which is commonly
referred to as a *jagged array*. This is because a two-dimensional array in Java is essentially an array
of arrays, allowing each inner array (or row) to have a different number of elements.
The statement that "the length of every 1D array may differ" in a two-dimensional array can be
demonstrated through the concept of jagged arrays. In contrast to rectangular arrays where each
row has the same number of columns, jagged arrays allow for flexibility in the number of elements
per row.
Example Program
jaggedArray[0][0] = 1;
jaggedArray[0][1] = 2;
jaggedArray[1][0] = 3;
jaggedArray[1][1] = 4;
jaggedArray[1][2] = 5;
jaggedArray[1][3] = 6;
jaggedArray[2][0] = 7;
jaggedArray[2][1] = 8;
jaggedArray[2][2] = 9;
System.out.println();
Output:
12
3456
789
Explanation:
- Declaration: The `jaggedArray` is declared as an array of arrays (`int[][]`), where each element can
be initialized separately.
- Accessing Elements: The program uses nested loops to access and print the elements of the jagged
array. Each inner loop iterates through the length of the current row, demonstrating that each row
can indeed have a different number of columns.
49. Frame a class Car which will contain a data member (id) which represents id of each car and a
static variable count whose value indicates total number of cars participating in the race. Write a
program in Java which will frame the above Car class and create three Car class objects and assign
their id through constructor. Also, add a static method carcount() inside the car class which will
display total number of cars in the race. Add a not static method disp() inside the Car class to
display id of the car objects.
Ans:
this.id = id;
count++;
car1.disp();
car2.disp();
car3.disp();
Car.carCount();
}
}
Output:
Ans: