Java Preparation Unit - 2
Java Preparation Unit - 2
Java Preparation Unit - 2
Unit-2
Derived Syntactical constructs in java
2 Marks
1. Explain constructor with suitable example.
OR
Define Constructor. List its types.
Ans) • Constructor is a special member function used to initialize the object.
• A constructor has the same name as the class.
• It doesn't have a return type, not even void.
For Example:
class Test
{
Test()
{
// constructor body
}
}
Object:
• An object is an instance of a class.
• It represents a specific instance of the class, with its own unique identity, state,
and behavior.
• Objects encapsulate data and behavior defined by their class.
Common Example:
public class Car {
String make;
String model;
int year;
}
public class Main
{
public static void main(String[] args)
{
Car car1 = new Car();
car1.make = "Toyota";
car1.model = "Camry";
car1.year = 2020;
}
}
4 Marks
1. Compare array and vector. Explain elementAt() and addElement() method.
OR
Difference between array and vector.
Ans) Difference between array and vector:
2. Explain the type of constructor in java and suitable example of copy constructor.
OR
Explain the types of constructors in Java with suitable example.
OR
What is constructor? List types of constructor. Explain parameterized constructor
with suitable example.
OR
Explain the type of constructor with example of parameterized constructor.
Ans) Constructor:
• Constructor is a special member function used to initialize the object.
• A constructor has the same name as the class.
• It doesn't have a return type, not even void.
1. Default Constructor:
• A default constructor is provided by Java if no constructor is explicitly defined in
a class.
• It initializes the object with default values (e.g., 0 for numeric types, null for
reference types).
• The default constructor has no parameters.
• Example:
public class MyClass {
int value;
public MyClass() {
value = 0;
}
3. Copy Constructor:
• A copy constructor is used to create a new object as a copy of an existing object.
• It initializes the new object with the values of another object of the same class.
• Copy constructors take an object of the same class as a parameter.
• Example:
public class MyClass {
int value;
public MyClass() {
value = 0;
}
MyClass.staticMethod();
System.out.println(MyClass.staticValue);
}
}
2. Final Method:
• A final method is a method that cannot be overridden by subclasses.
• Once a method is declared final in a superclass, subclasses cannot override it.
• Final methods provide stability and security in class hierarchies by preventing
modification of critical behavior.
• Final methods are typically used to define immutable behavior or critical
functionality that should not be altered.
• Syntax: final returnType methodName(parameters) { // Method body }
• Example:
class Parent
{
final void display() {
System.out.println("Displaying from Parent");
}
}
6. List any four methods of string class and state the use of each.
Ans) Methods of string class:
1. charAt(int index):
• This method returns the character at the specified index within the string.
• Example:
String str = "Hello";
char ch = str.charAt(0); // Returns 'H'
2. length():
• This method returns the length of the string.
• Example use:
String str = "Hello";
int length = str.length(); // Returns 5
3. substring(int beginIndex):
• This method returns a new string that is a substring of the original string,
starting from the specified index.
• Example use:
String str = "Hello World";
String substr = str.substring(6); // Returns "World"
4. indexOf(String str):
• This method returns the index of the first occurrence of the specified
substring within the string, or -1 if the substring is not found.
• Example use:
String str = "Hello World";
int index = str.indexOf("World"); // Returns 6
7. List any four methods of string buffer class and state the use of each.
Ans) 1. append(String str):
• This method appends the specified string to the end of the StringBuffer
object.
• Example use:
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World"); // Result: "Hello World"
4. reverse():
• This method reverses the characters in the StringBuffer object.
• Example use:
StringBuffer sb = new StringBuffer("Hello");
sb.reverse(); // Result: "olleH"
8. Any four method of vector class with example.
OR
Describe the use of any methods vector class with their syntax.
Ans) 1. addElement(E element):
• This method appends the specified element to the end of the vector.
• Example:
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
3. removeElementAt(int index):
• This method removes the element at the specified index from the vector.
• Example:
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
vector.removeElementAt(1); // Removes the element at index 1 (20)
4. clear():
• This method removes all elements from the vector.
• Example:
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
vector.clear(); // Removes all elements
5. indexOf(Object o):
• This method returns the index of the first occurrence of the specified
element in the vector, or -1 if the element is not found.
• Example:
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
int index = vector.indexOf(20); // Returns 1
6. equals(Object obj):
• This method compares the specified object with the vector for equality.
• Example:
Vector<Integer> vector1 = new Vector<>();
vector1.addElement(10);
vector1.addElement(20);
Example:
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
6 Marks
1. Explain the command line arguments with suitable example.
Ans) • Command-line arguments are parameters passed to a program when it is executed
from the command line or terminal.
• These arguments provide input data to the program and can be accessed within the
program's main method.
• In Java, command-line arguments are stored as strings in the args parameter of the
main method.
When running this program from the command line, you can provide command-line
arguments separated by spaces.
For example:
javac CommandLineExample.java
java CommandLineExample arg1 arg2 arg3
Output:
Number of arguments: 3
Argument 1: arg1
Argument 2: arg2
Argument 3: arg3
2. Explain vector with the help of example. Explain any 3 methods of vector
class.
Ans) Vector:
• A Vector in Java is a dynamic array-like data structure that allows elements to be
added or removed dynamically.
• It is similar to an ArrayList but is synchronized, making it thread-safe.
• Vectors can hold elements of any data type.
• They automatically resize themselves as needed.
Example:
import java.util.Vector;
Methods of Vectors:
1. addElement(E element):
• This method appends the specified element to the end of the vector.
• Example:
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
3. removeElementAt(int index):
• This method removes the element at the specified index from the vector.
• Example:
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
vector.removeElementAt(1); // Removes the element at index 1 (20)
4. clear():
• This method removes all elements from the vector.
• Example:
Vector<Integer> vector = new Vector<>();
vector.addElement(10);
vector.addElement(20);
vector.clear(); // Removes all elements