0% found this document useful (0 votes)
32 views16 pages

Java Questions

Java Questions
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
32 views16 pages

Java Questions

Java Questions
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 16

Q.1.

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.7. What will be the output of the below code?


class Demo {
static int p;
static {
p = 11;
System.out.print(p + " ");
}
static{
p++;
System.out.print(p + " ");
}
public static void main(String[] args) {
p++;
System.out.println(p);
}
}
Q.8. Write a method power() to calculate 2 raised to the power n without using any
built in method or arithmetic operators.

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.

Q.12. What will be the output of the following java code?


class Test
{
public static void main(String[] args)
{
int x = 5;
boolean res = (x < 4) & (++x < 10);
System.out.println("x="+x);
System.out.println("res="+res);
}
}
Q.13. What will be the output of the following java code?
class Test
{
public static void main(String[] args)
{
int x = 5;
boolean res = (x < 4) && (++x < 10);
System.out.println("x="+x);
System.out.println("res="+res);
}
}
Q.14. Difference between Logical AND(&&) and Bitwise AND(& ) operator.

Q.15. What will be the output of the following java code?


class Test
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
System.out.println(50>>i);
}
}

Q.16. What will be the output of the following java code?


class Test
{
public static void main(String[] args)
{
for(int i=0;i<5;i++)
System.out.println(10<<i);
}
}
Q.17. What will be the output of the following code?
class Test {
public static void main(String[] args) {
int a = 10;
int b = ++a;
int c = a++;
System.out.printf("a: %d \nb: %d \nc: %d", a, b, c);
}
}

Q.18. What will be the output of the below code?


public class Test {
public static void main(String args[]) {
int age=20;
boolean flag;
flag=(age>20)?true:false;
System.out.println("age=" + flag);
}
}
Q.19. What is the output of the following Java program?
class SIT
{
public static void main (String args[])
{
System.out.println(10 + 20 + "SILICON");
System.out.println("SILICON" + 10 + 20);
}
}
Q.20. What is the purpose of static methods and variables?
Q.21. Define JVM and its need?
Q.22. What will be the output?

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;

// Constructor for rectangle

Figure(double len, double wid) {

l = len;

w = wid;

// Method to calculate area

double findArea() {

return( l * w);

// Class for Square

class Square extends Figure {

// Constructor for square

Square(double sidelen) {

super(sidelen, sidelen); // Square has equal length and width

}
// Class for Rectangle

class Rectangle extends Figure {

// Constructor for rectangle

public Rectangle(double length, double width) {

super(length, width);

// Driver class to test functionalities

class FigureTest {

public static void main(String[] args) {

// Create a square

Square sqr = new Square(4);

System.out.println("Area of the square: " + sqr.findArea()); // Should print 16.0

// Create a rectangle

Rectangle rect = new Rectangle(4, 6);

System.out.println("Area of the rectangle: " + rect.findArea()); // Should print 24.0

}
}

Q.2.

import java.util.Scanner;

class Student {

String regNo;

private String stdName;

private String branch;

private double CGPA;


// Method to accept student details

public void acceptDetails() {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter Registration Number: ");

regNo = scanner.nextLine();

System.out.print("Enter Student Name: ");

stdName = scanner.nextLine();

System.out.print("Enter Branch: ");

branch = scanner.nextLine();

System.out.print("Enter CGPA: ");

CGPA = scanner.nextDouble();

scanner.nextLine(); // Consume newline

// Method to display student details

public void displayDetails() {

System.out.println("Registration Number: " + regNo);

System.out.println("Student Name: " + stdName);

System.out.println("Branch: " + branch);

System.out.println("CGPA: " + CGPA);

System.out.println("--------------------------");

}
class StudentDetails {

public static void main(String[] args) {

final int NUMBER_OF_STUDENTS = 20;

Student[] stud = new Student[NUMBER_OF_STUDENTS];

// Accept details for each student

for (int i = 0; i < NUMBER_OF_STUDENTS; i++) {

System.out.println("Entering details for Student " + (i + 1) + ":");

stud[i] = new Student();

stud[i].acceptDetails();

System.out.println();

// Display details for each student

System.out.println("Student Details:");

for (int i = 0; i < NUMBER_OF_STUDENTS; i++) {

System.out.println("Details for Student " + (i + 1) + ":");

stud[i].displayDetails();

}
}

Q.3.

import java.util.Scanner;

class Area {

// Member variables

double length;

double breadth;
// Method to initialize length and breadth

public void setDim(double length, double breadth) {

this.length = length;

this.breadth = breadth;

// Method to calculate and return the area

public double getArea() {

return length * breadth;

class Test {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in)

// Create an object of Area class

Area rectangle = new Area();

// Input dimensions

System.out.print("Enter the length of the rectangle: ");

double length = scanner.nextDouble();

System.out.print("Enter the breadth of the rectangle: ");

double breadth = scanner.nextDouble();

// Set dimensions

rectangle.setDim(length, breadth);

// Calculate and display the area

double area = rectangle.getArea();

System.out.println("The area of the rectangle is: " + area);


}
}

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

Employee(int empId, String empName, double salary) {

empId = empId;

empName = empName;

salary = salary;

// Method to display employee details

void display() {

System.out.println("Employee ID: " + empId);

System.out.println("Employee Name: " + empName);

System.out.println("Salary: " + salary);

}
class EmployeeTest {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

Employee[] emp = new Employee[5]; // Array to hold 5 Employee objects

// Read details for 5 employees

for(int i=0;i<5;i++){

System.out.println("Enter details for Employee " + (i + 1) + ":");

System.out.print("Employee ID: ");

int empId = scan.nextInt();

scan.nextLine(); // Consume newline

System.out.print("Employee Name: ");

String empName = scan.nextLine();

System.out.print("Employee Salary: ");

double salary = scan.nextDouble();

// Create a new Employee object and store it in the array

emp[i] = new Employee(empId, empName, salary);

// Display the details

for(int i=0;i<5;i++)

emp[i].display()

}
Q.14.

Logical AND operator (&&)

 Used for logical operations, primarily with boolean values.

 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.

 For boolean operands, it performs a logical AND.


 For numeric types, it performs a bitwise operation on each bit.

 For boolean operands, returns a boolean value.


 For numeric types, returns a value of the same type.

Q.29. // Abstract class GShape


abstract class GShape {
protected double dim1;
protected double dim2;

// Constructor
GShape(double dim1, double dim2) {
dim1 = dim1;
dim2 = dim2;
}

// Abstract method for area


abstract double Area();
}

// Concrete class Trapezium


class Trapezium extends GShape {
Trapezium(double a, double b, double h) {
super(a, b); // dim1 = a, dim2 = b
dim2 = h; // dim2 holds height
}
double Area() {
return 0.5 * (dim1 + dim2) * dim2; // Area = 1/2 * (a + b) * h
}
}

// Concrete class Ellipse


class Ellipse extends GShape {
Ellipse(double a, double b) {
super(a, b); // dim1 = a, dim2 = b
}
double Area() {
return 3.14 * dim1 * dim2; // Area = 3.14 * a * b
}
}
// Driver class
public class Driver {
public static void main(String[] args) {
GShape shape1 = new Trapezium(5, 3, 4); // a = 5, b = 3, height = 4
GShape shape2 = new Ellipse(3, 4); // a = 3, b = 4
System.out.println("Area of Trapezium: " + shape1.Area());
System.out.println("Area of Ellipse: " + shape2.Area());
}
}

Q.30. // Parent class Student

class Student {

protected int SID;

protected String name;

// Parameterized constructor

Student(int studentID, String nm) {

SID = studentID;

name = nm;

// Method to display student details

public void display() {

System.out.println("Student ID: " + SID);

System.out.println("Name: " + name);

// Child class UGstudent

class UGstudent extends Student {

int noOfSemesters;

double cgpa;

// Parameterized constructor

UGstudent(int studentID, String name, int ns, double cg) {


super (studentID, name); // Calling the parent class constructor

noOfSemesters = ns;

cgpa = cg;

// Overriding display method to include UGstudent details

public void display() {

super.display(); // Call the display method of the parent class

System.out.println("Number of Semesters: " + noOfSemesters);

System.out.println("CGPA: " + cgpa);

// Driver class

public class Driver {

public static void main(String[] args) {

// Creating an instance of UGstudent

UGstudent ugS= new UGstudent(101, "Alice", 6, 8.5)

// Displaying the student details

ugS.display();

}
}

You might also like