JAVA QB Answers
JAVA QB Answers
JAVA QB Answers
Integer types can hold whole numbers such as 123.-96, and 5639. Java supports four types of
integers . They are byte, short, int, and long. Java does not support the concept of unsigned types
and therefore all Java values are signed meaning they can be positive or negative. Table 4.2 shows
the memory size and range of all the four integer data type
8. Explain automatic type conversion with suitable
code example.
12. Explain the increment, decrement and conditional operator with syntax and example.
Increment and Decrement Operators
The unary increment and decrement operators ++ and -- comes in two forms, prefix and postfix.
They perform two operations. They increment (or decrement) their operand, and return a value
for use in some larger expression.
In prefix form, they modify their operand and then produce the new value.
In postfix form, they produce their operand’s original value, but modify the operand in the
background. The operator ++ adds 1 to the operand while – subtracts 1. Both are unary operators
and are used in the following form:
++m; OR m++;
--m; or m--; m=5;
y= ++m; In this case, the value of y and m would be 6.
If m=5; y= m++; then, the value of y would be 5 and m would be 6.
13. Explain the process of reading a character from the keyboard with suitable example.
Arrays.fill(numbers, 0);
12. Explain the wrapper class methods String() , valueOf() and parsing methods with syntax and
example.
Here are the explanations of the three wrapper class methods you mentioned:
1. `String()`: This method creates a new string object with the specified value. For example:
String str = new String("Hello, World!");
2. `valueOf()`: This method returns a string representation of the specified value. For example:
int num = 42;
String str = String.valueOf(num); // str is "42"
3. Parsing methods: These methods convert a string representation of a value to the corresponding
primitive data type. There are several parsing methods available for each primitive data type:
- `Byte.parseByte()`: Parses a string to a byte.
- `Short.parseShort()`: Parses a string to a short.
- `Integer.parseInt()`: Parses a string to an int.
- `Long.parseLong()`: Parses a string to a long.
- `Float.parseFloat()`: Parses a string to a float.
- `Double.parseDouble()`: Parses a string to a double.
- `Boolean.parseBoolean()`: Parses a string to a boolean.
For example:
String str = "42";
int num = Integer.parseInt(str); // num is 42
13. Write the general form of a class? Explain how to define a class in Java with suitable example.
The General Form of a Class
A class is declared by use of the class keyword.
The data, or variables, defined within a class are called instance variables.
The code is contained within methods.
Collectively, the methods and variables defined within a class are called members of the class.
14. Explain how to create an object in Java with suitable example.
18. Explain how to return an object from a method with suitable example
In Java, you can return an object from a method by specifying the return type of the method as the
class of the object. For example:
// Define a class
class Person {
String name;
int age;
}
// Define a method that returns an object of the class
public static Person createPerson(String name, int age) {
Person person = new Person();
person.name = name;
person.age = age;
return person;
}
// Call the method and get the returned object
Person person = createPerson("John", 30);
In this example, we define a class called `Person` with two fields: `name` and `age`. We then define
a method called `createPerson` that takes two parameters (`name` and `age`) and returns an object
of the `Person` class. Inside the method, we create a new `Person` object, set its fields to the values
of the parameters, and return it.
We can then call the `createPerson` method and get the returned `Person` object.
class MyClass {
static int count = 0;
In this example, we define a class called MyClass with a static variable called count and
a static method called incrementCount. The incrementCount method increments
the value of the count variable. We can then call the incrementCount method without
creating an instance of the MyClass class.
22. Create a class Student with members Register number, name and marks in three subjects.
Use constructors to initialize objects and write a method that displays the information of a
student.
class Student {
int regNo;
String name;
int marks1;
int marks2;
int marks3;
public Student(int regNo, String name, int marks1, int marks2, int marks3) {
this.regNo = regNo;
this.name = name;
this.marks1 = marks1;
this.marks2 = marks2;
this.marks3 = marks3;
}
public void display() {
System.out.println("Register Number: " + regNo);
System.out.println("Name: " + name);
System.out.println("Marks in Subject 1: " + marks1);
System.out.println("Marks in Subject 2: " + marks2);
System.out.println("Marks in Subject 3: " + marks3);
}
}
// Create an object of the Student class
Student student = new Student(1234, "John Doe", 90, 85, 95);
// Call the display method to display the student's information
student.display();
UNIT III
Questions carrying 2 marks
1. What is inheritance?
Inheritance is another aspect of OOP paradigm.
• The mechanism of deriving a new class from an old one is called Inheritance.
• The old class is known as the BASE CLASS OR SUPER CLASS OR PARENT CLASS.
• The new class derived is called the SUBCLASS OR DERIVED CLASS.
• The inheritance allows subclasses to inherit all the variables and methods of their parent classes.
2. What is the purpose of keyword extends?
The keyword extends signifies that the properties of the superclassname are extended to the
subclassname.
class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking");
}
}
public class Main
{
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // inherited from Animal class
dog.bark();
}
}
3. What is the purpose of super()?
Using Super
The super keyword is similar to this keyword.
Following are the scenarios where the super keyword is used.
• Super is used to invoke the superclass constructor from subclass.
•It is used to differentiate the members of superclass from the members of subclass, if they have
same names. i.e. to access a member of the superclass that has been hidden by a member of a
subclass.
4. What are abstract classes?
Multilevel inheritance is a type of inheritance where a subclass inherits properties and methods from
a superclass, which in turn inherits properties and methods from another superclass. In Java, you
can achieve multilevel inheritance by using the `extends` keyword.
Here's an example:
class Animal {
public void eat() {
System.out.println("I can eat");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("I can bark");
}
}
class Labrador extends Dog {
public void color() {
System.out.println("I am brown in color");
}
}
public class Main {
public static void main(String[] args) {
Labrador myLabrador = new Labrador();
myLabrador.eat();
myLabrador.bark();
myLabrador.color();
}
}
In this example, the `Labrador` class inherits the properties and methods of the `Dog` class which
in turn inherits the properties and methods of the `Animal` class. The `Labrador` class has its own
method called `color()` which is not present in the `Animal` or `Dog` classes.
4. Explain the purpose abstract and final classes.
The superclasses are more general than their subclasses. Usually, the superclasses are made
abstract so that the objects of its prototype cannot be made. So the objects of only the subclasses
can be used. To make a class abstract, the keyword abstract is used in the class definition.
The abstract class cannot be instantiated because it does not define a complete implementation.
public abstract class
{
….
}
Using Final with class:
We can also prevent inheritance by making a class final. When a class is declared as final, its
methods also become final. An abstract class cannot be declared as final because an abstract class
is incomplete and its subclasses need to provide the implementation.
5. Explain how to create and use a package in Java with suitable example.
6. List and explain different API packages available in Java.
In Java there are different API packages or System Packages, like
• lang • util • io • awt • Net • applet
The `java.lang` package is one of the most important packages in Java. It contains classes that are
fundamental to the design of the Java programming language. Some of the classes included in this
package are:
1.Object: This is the root class of the Java class hierarchy. All other classes are subclasses of this
class.
2. String: This class represents a sequence of characters.
3. System: This class provides access to system resources such as standard input, standard output,
and error output.
4. Math: This class provides mathematical functions such as trigonometric functions, exponential
functions, and logarithmic functions.
5. Thread: This class provides support for multithreaded programming.
6. Exception: This class is the superclass of all exceptions.
7. RuntimeException: This class is the superclass of all runtime exceptions.
The `java.util` package contains classes that are used for utility purposes such as data structures,
date and time manipulation, and input/output operations. Some of the classes included in this
package are:
1. **ArrayList**: This class provides a resizable array implementation.
2. **LinkedList**: This class provides a linked list implementation.
3. **HashMap**: This class provides a hash table implementation.
4. **Date**: This class represents a specific instant in time.
5. **Calendar**: This class provides methods for manipulating dates and times.
6. **Scanner**: This class provides methods for reading input from various sources.
7. **Random**: This class provides methods for generating random numbers.
The `java.io` package provides classes for performing input and output (I/O) operations in Java.
Some of the classes included in this package are:
1. **File**: This class provides methods for working with files and directories.
2. **InputStream**: This class provides methods for reading input from various sources.
3. **OutputStream**: This class provides methods for writing output to various destinations.
4. **Reader**: This class provides methods for reading character-based input.
5. **Writer**: This class provides methods for writing character-based output.
6. **BufferedReader**: This class provides buffered character input.
7. **BufferedWriter**: This class provides buffered character output.
The `java.awt` package provides classes for creating graphical user interfaces (GUIs) in Java. Some
of the classes included in this package are:
1. **Frame**: This class provides a top-level window for displaying other components.
2. **Button**: This class provides a push button component.
3. **Label**: This class provides a non-editable text display.
4. **TextField**: This class provides a text input component.
5. **TextArea**: This class provides a multi-line text input component.
6. **Checkbox**: This class provides a check box component.
7. **Choice**: This class provides a drop-down list component.
Unit IV
The `setText()` method is used to set the text of a component. For example, if you have a `JButton`
object named `button`, you can set its text using the following code: `button.setText("Click me!");`.
The `getText()` method is used to retrieve the text of a component. For example, if you have a
`JTextField` object named `textField`, you can retrieve its text using the following code: `String
text = textField.getText();`.
18. List any four swing components.
Here are four Swing components in Java:
- `JButton`: A button that can be clicked by the user.
- `JTextField`: A text field that allows the user to enter text.
- `JLabel`: A component that displays a single line of read-only text.
- `JComboBox`: A drop-down list that allows the user to select one item from a list of items.
19. Differentiate Component and Container.
In Java, a `Component` is an abstract class that represents a graphical user interface (GUI)
component. It is the base class for all AWT components. A `Container`, on the other hand, is a
subclass of `Component` that can contain other components and containers.
In other words, a `Component` is an independent visual control, such as a push button or slider,
while a `Container` holds a group of components. Examples of containers are `JPanel`, `JFrame`,
and `JApplet`. Examples of components are `JLabel`, `JTextField`, and `JButton`.
10. Explain the use of JList and any for methods associated with it.
11. Illustrate the use of JLabel with suitable example.
12. List and explain different methods associated with JCheckBox.
13. Explain the use of JCombobox and any for methods associated with it.
14. Explain different methods associate with JRadioButton control with suitable example.