Java Questions
Java Questions
Write a program to define a class 'Figure' that displays the area of rectangle and
square. The class has data members length and width. Define constructors to
initialise the dimensions of the figure and a method findArea() that returns the area
of the figure. Create a driver class to create the objects of Square and Rectangle and
test all the functionalities.
Q.2. Write a program to define a class Student having the attribute regNo, stdName,
branch and CGPA. The class has two methods to accept and display the student
details. Read the details of 20 students using an array of Student class object.
Q.3. Write a program to create a class 'Area' with member length and breadth. The
class has two methods, 'setDim ()' initialises length and breadth and 'getArea ()'
returns the area of the rectangle. Create a driver class to create the objects of class
'Area' and test the functionalities of the Area class.
Q.4. Write a java program to define a class Employee with data members empId,
empName and salary. The class has default and parameterized constructor to
initialize the members of the class and a method display() to show the employee
details. Create another driver class to read and display the details of 5 employees
using an array of Employee class object.
Q.5. Find the output of the code:
class Sample{
Sample(){
System.out.println("Sample constructor");
}
Sample(int x){
this();
System.out.println(x);
}
}
class Demo4{
public static void main(String args[]){
Sample a = new Sample(50);
}
}
Q.6. Predict the output of the following code. Justify it.
class Demo{
int rollno;
static String college="Silicon";
Demo(int r){
rollno=r;
}
static void change(){
college = "SIT";
rollno=5;
}
void display (){
System.out.println(rollno+" "+college);
}
}
class Test{
public static void main(String args[]){
Demo ob=new Demo(10);
Demo.change();
ob.display();
}
}
Q.9. You have define a parameterized constructor in your class. Can you still create
an object by calling the default constructor?
Q.10. Differentiate between class variable and instance variable with example.
Q.11. Can you overload main() in java? Explain it with a suitable program.
class B{
B(){
System.out.print("Hello B");
}
B(double a){
this();
System.out.print("Hello");
}
B(int a){
this();
System.out.print(a);
}
}
class Demo {
public static void main(String []args) {
B b1=new B(25);
System.out.print("use of this keyword");
}
}
Q.23.
What will be the output of the below code?
class Demo {
static int a = 10;
}
class Driver {
public static void main(String []args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.a = 20;
System.out.print( d2.a);
}
}
Q,24. What will be the output of the below code?
class Example{
static int num;
static {
num = 68;
}
static {
System.out.println("Programming");
num = 67;
}
public static void main(String args[]) {
System.out.println("Value of num: "+num);
}}
Q.25. What will be the output of the following code if the command line arguments
are provided as:
java Test 10 20 30 40
class Test{
public static void main(String args[]){
System.out.println(args[2]);
}
}
Q.24. With a suitable example explain the use of static block in java.
Q.25. Explain the method overloading concept of java with a suitable example.
Q.26. What will be the output of the below program:
class A
{
A(){ System.out.println(" A class Const"); }
}
class B extends A
{
B(){ System.out.println(" B class Const"); }
}
class C extends B
{
C(){ System.out.println(" C class Const"); }
}
class D extends C
{
D(){ System.out.println(" D class Const"); }
}
class E extends C
{
E(){ System.out.println(" E class Const"); }
}
class Test{
public static void main( String args[]){
D ob=new D( );
}
}
Q.27. Justify the output of the following code:
class Vehicle{
final void sell(){
System.out.println("Sold out");
}
}
class Car extends Vehicle {
void sell() {
System.out.println("Only 1 Car left");
}
public static void main(String args[]){
Car obj= new Car();
obj.sell();
}
}
Q.28. Find the output for the following code:
class Foo {
public int i;
private int j;
}
class Bar extends Foo {
void display() {
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class Testo {
public static void main(String args[]) {
Bar obj = new Bar();
obj.i = 1;
obj.j = 2;
obj.display();
}
}
Q.29. Define an abstract class named as GShape, having data members dim1 and
dim2 and an abstract method Area(). Extend this class to create two concrete
classes named as Trapezium and Ellipse. Override the Area() method in the sub
classes. Invoke the Area() method in the main method of another Driver class
through an abstract class reference variable. (Consider area of Trapezium=
1/2(a+b)*h and area of ellipse= 3.14*a*b)
Q.30. Write a java program to define a class Student with data members: studentID,
name, and a method to display the student details. The class should have a
parameterized constructor to initialize the data members. Create a child class
UGstudent that contains data members: noOfsemesters and cgpa. Use ‘super’ in
child class constructor to initialize the data members. Create another Driver class to
test the functionalities.
Q.31. Write a java program to define a class Customer with data members:
customerID, name, and a method to display the customer details. The class should
have a parameterized constructor to initialize the data members. Create a child class
RegularCustomer that contains data members: mobileNo and prizeAmount. Use
‘super’ in child class constructor to initialize the data members. Create another
Driver class to test the functionalities.
Q.32. Write a java program to define a class Library with data members: memberID,
Name, and a method to display the member details. The class should have a
parameterized constructor to initialize the data members. Create a child class
studentMember that contains data members: noOfsemesters and noBooks. Use
‘super’ in child class constructor to initialize the data members . Create another
Driver class to test the functionalities.
Q.33. Define an abstract class named as GeometricShape, having data members
dimOne and dimTwo and an abstract method calculateArea(). Extend this class to
create two concrete classes named as Rectangle and Triangle. Override the
calculateArea() method in the sub classes. Invoke the calculateArea() method in the
main method of another Driver class through an abstract class reference variable.
Answers
Q.1. // Super class for figures
class Figure {
double l;
double w;
l = len;
w = wid;
double findArea() {
return( l * w);
Square(double sidelen) {
}
// Class for Rectangle
super(length, width);
class FigureTest {
// Create a square
// Create a rectangle
}
}
Q.2.
import java.util.Scanner;
class Student {
String regNo;
regNo = scanner.nextLine();
stdName = scanner.nextLine();
branch = scanner.nextLine();
CGPA = scanner.nextDouble();
System.out.println("--------------------------");
}
class StudentDetails {
stud[i].acceptDetails();
System.out.println();
System.out.println("Student Details:");
stud[i].displayDetails();
}
}
Q.3.
import java.util.Scanner;
class Area {
// Member variables
double length;
double breadth;
// Method to initialize length and breadth
this.length = length;
this.breadth = breadth;
class Test {
// Input dimensions
// Set dimensions
rectangle.setDim(length, breadth);
Q.4.
import java.util.Scanner;
class Employee
int empId;
String empName;
double salary;
// Default constructor
Employee() {
empId = 0;
empName = "Unknown";
salary = 0.0;
// Parameterized constructor
empId = empId;
empName = empName;
salary = salary;
void display() {
}
class EmployeeTest {
for(int i=0;i<5;i++){
for(int i=0;i<5;i++)
emp[i].display()
}
Q.14.
The && operator is a short-circuit operator. If the left operand evaluates to false, the right
operand is not evaluated because the entire expression will be false regardless.
Typically used with boolean expressions.
Returns a boolean value (true or false).
Bitwise AND operator (&)
Used for bitwise operations on integer types (like int, byte, etc.) as well as for boolean
values.
The & operator evaluates both operands regardless of the first operand's value.
It Can be used with boolean values and numeric types.
// Constructor
GShape(double dim1, double dim2) {
dim1 = dim1;
dim2 = dim2;
}
class Student {
// Parameterized constructor
SID = studentID;
name = nm;
int noOfSemesters;
double cgpa;
// Parameterized constructor
noOfSemesters = ns;
cgpa = cg;
// Driver class
ugS.display();
}
}