Java
– Unit 2 Answers
Very
Short Answers
1.
What is Loop? Why do we use loops in a program?
A
loop is used to execute a
block of code repeatedly. It helps in reducing code redundancy and automates
tasks like traversing arrays or performing repeated calculations.
2.
What is Polymorphism?
Polymorphism allows one action
to behave differently based on the object. It is achieved through method
overloading and method overriding, enabling flexibility and code reuse.
3.
What is Constructor?
A
constructor is a special
method used to initialize objects. It has the same name as the class and does
not have a return type. It runs automatically when an object is created.
4.
What is Static Member? Explain with suitable program.
Static members belong to the
class, not objects.
class Test { static int count = 0;
}
count is shared by all objects of the class.
5.
Discuss about the Switch Case in detail.
The switch statement tests a variable against
multiple case values. It executes the matching block and uses break to exit. It
is an alternative to multiple ifelse conditions.
6.
What is Pre-test and Post-test loop? Pre-test loop (like while)
checks condition before executing.
Post-test loop (do-while) executes the block first
and checks condition after.
Short Answers
1.
Explain how to declare and create object.
A class must be defined first.
An object is created using the new keyword. Example:
class Car { }
Car myCar = new Car();
Here, Car is the class, and myCar is the object
created using new.
2.
Discuss about the methods with example.
Methods define the behavior of a
class. They contain code that runs when the method is called. Example: void greet() {
System.out.println("Hello");
}
Methods promote reusability and organization in
code.
3.
What is Visibility Control? Discuss in detail.
Visibility control defines
access levels for class members using access modifiers:
•
public: accessible
everywhere
•
private: accessible within
the class
•
protected: accessible
within package and subclass
•
Default (no modifier):
accessible within the package
This enhances security and encapsulation.
4.
Discuss about the Wrapper Classes. Wrapper classes convert
primitive types to objects. For example, int to Integer, char to Character. Example:
Integer x = new Integer(10);
They are useful in collections, as they work with
objects.
5.
Explain Nesting of Methods with Example.
Nesting
methods means calling one
method inside
another. Example: void outer()
{ inner();
}
void inner() {
System.out.println("Inner method");
}
It helps in breaking complex logic into smaller
parts.
6.
What is String? Write how to create string in Java program.
A String is a sequence of
characters. Ways to create:
String s1 = "Hello";
String s2 = new String("World");
It is immutable and part of java.lang package.
7.
What are the types of constructor?
Explain Constructor Overloading. Types:
•
Default Constructor
•
Parameterized Constructor
Constructor
Overloading means having multiple constructors in a class with different
parameters.
class Example { Example() { }
Example(int x) { }
}
Long
Answers
1. Explain the
different control structures used in Java. Java has three types of control
structures:
•
Sequential: Statements are executed one after another.
•
Selection (Decision): Based on conditions (if, if-else, switch)
•
Iteration (Looping): Repeat blocks of code (for, while, do-while)
These structures help manage the flow of execution
and improve code logic and efficiency.
2. Explain if statement with example and
syntax.
The if
statement is used to execute a
block of code only if a condition
is true. Syntax: if(condition) {
// code
}
Example:
int a = 10; if (a > 5) {
System.out.println("Greater than 5");
}
3. Describe if-else
and if-else-if with example.
•
if-else: Provides two branches — if true, one block runs; else
another.
if (a > b) {
System.out.println("A is greater");
} else {
System.out.println("B is greater");
}
•
if-else-if: Multiple conditions can be tested.
if (a > b) { ... } else if (a ==
b) { ... } else { ... }
4. Explain for, while,
and do-while loop with example.
•
for loop:
for(int i = 0; i < 5; i++) {
System.out.println(i);
}
•
while loop: int i = 0; while(i < 5) {
System.out.println(i); i++;
}
•
do-while loop:
int i = 0; do {
System.out.println(i); i++;
} while(i < 5);
5. Discuss the
continue and break statement with example.
• break: exits the loop
early
for(int i=1;i<=5;i++) { if(i==3) break;
System.out.println(i);
}
• continue: skips
current iteration for(int i=1;i<=5;i++) {
if(i==3) continue;
System.out.println(i);
}
6.
Explain how to create string? Explain about immutable string. A
string can be created using:
String str1 = "Hello";
String str2 = new String("World"); Immutability: Once created, strings
cannot be changed. Any operation returns a new object.
String a = "Java";
a.concat("Programming"); // "a"
still contains "Java"
7.
What is difference in string functions? Explain with example.
Common String functions:
•
length(): returns string
length
•
charAt(index): returns char
at index
•
equals(): compares two
strings
•
substring(): returns a part
of string Example:
String s = "Java"; System.out.println(s.length());
// 4
8. What is function
and operator overloading? Explain in detail.
•
Function Overloading: Multiple methods with the same name but
different parameters.
void show() {} void show(int a) {}
•
Operator Overloading: Not supported in Java like C++, except + is
overloaded for strings.
String s = "Hello" + "World";
9.
Difference between Overloading and Overriding
Overloading Overriding
Same method name, Same
method, same different parameters parameters
Happens in same class Happens in subclass
Compile-time Runtime polymorphism polymorphism
10.
Explain final classes.
A final class cannot be
extended. Example:
final class A { }
class B extends A { } // Error
It is used to prevent inheritance for security or
design reasons.
11.
What is Loop? Explain types with example.
Loops are used for repeating tasks. Types:
•
for loop
•
while loop
•
do-while loop Example:
for(int i=0;i<3;i++) {
System.out.println("Hi");
}
12. Define
Array and explain types.
An array is a collection of similar elements. Types:
•
One-dimensional array int[] a = new int[5];
•
Multi-dimensional array int[][] b = new int[3][3];
Arrays store data efficiently using indexing.
13. Write
a program to print
“Welcome” 10 times public class
WelcomePrinter { public static void
main(String[] args) { for(int i =
1; i <= 10; i++) {
System.out.println("Welcome");
}
}
}

0 Comments