0% found this document useful (0 votes)
29 views2 pages

Sample Questions For Midterm Exam - CSE215 (Sec 19) - Spring2024

The document contains a Java program with multiple classes and methods. It performs calculations on integers and doubles, printing the results. It also contains examples of syntax errors and how to define a Student class with constructors and a method to display student details.

Uploaded by

hasibul rojan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views2 pages

Sample Questions For Midterm Exam - CSE215 (Sec 19) - Spring2024

The document contains a Java program with multiple classes and methods. It performs calculations on integers and doubles, printing the results. It also contains examples of syntax errors and how to define a Student class with constructors and a method to display student details.

Uploaded by

hasibul rojan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Analyzing the given Java program, determine the output produced by the program when
executed. Your output format should match actual output that will be produced by the
program on computer screen.

class Calculation {
int performCalculation (int a, int b) {
System.out.println("Performing integer calculation");
return a + b;
}

double performCalculation(double a, double b) {


System.out.println("Performing double calculation");
return a * b;
}
}

public class Main {


public static void main (String[] args) {
Calculation calculator = new Calculation();
int result1 = calculator.performCalculation(5, 3);
double result2 = calculator.performCalculation(2.5, 1.5);
double result3 = calculator.performCalculation(2, 1.5);

System.out.println("Result 1: " + result1);


System.out.println("Result 2: " + result2);
System.out.println("Result 3: " + result3);
}
}

2. Identify all the syntax errors (i.e., compilation-time errors) from the following Java
program and write the corrected code.

class Person{
private String name;

void displayInfo() {
System.println("Name: " + name);
}
}

public class Main{


public void main(String[] args) {
Person person1 = Person();
Person1.name = 'Alice';
person1.displayInfo();

}
}
3. Write a Java program to create a class called Student that represents a student's ]
information. The class should have the following attributes:

• name (String): The name of the student.


• id (int): The unique identification number of the student.
• age (int): The age of the student.

Implement two constructors for the Student class:

1. Parameterless Constructor: A constructor without parameters that initializes


the name to "Unknown", id to 0, and age to 0.

2. Parameterized Constructor: A constructor with parameters that allows the


user to initialize the name, id, and age attributes.

Include a method named displayInfo() in the Student class that displays the
student's information in the following format:

Student Name: [name]


Student ID: [id]
Student Age: [age]

In the Main class, create two Student objects: one using the parameterless
constructor and another using the parameterized constructor. Display the information of
both students using the displayInfo() method.

Example Output:

Student Name: Unknown


Student ID: 0
Student Age: 0

Student Name: John Doe


Student ID: 12345
Student Age: 18

You might also like