Preet raj Core Java and Databases CS4PR Time Allotted: 3 Hours Final Exam: Total Possible Points 75 Q1. What is difference between overloading and overriding? 10 points a) In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method. b) Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass. c) In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass. d) Overloading must have different method signatures whereas overriding must have same signature. Q2. What is Compile Time Polymorphism in OOPS? 5 points Compile time Polymorphism also known as method overloading Method overloading means having two or more methods with the same name but with different signatures Q3. Write a program that will take some positive float type numbers from the keyboard and find their summation. If any negative number is input, then your program should handle it with a user-defined exception. 10 points import java.util.scanner; class NegativeNumberException extends Exception { String a; NegativeNumberException(String x) { a = x; public String tostring() { return "Error! Negative number found: " + a; public class UserDefinedException { static float check(float x) throws NegativeNumberException {
if (x <0) { throw new NegativeNumberException(Integer.toString(x)); else { return x; public static void main(string[] args) { Scanner in = new Scanner(System.in); float sum = 0; try { while (in.hasnextfloat()) { sum += check(in.nextfloat()); catch (NegativeNumberException e) { System.out.println(e); System.out.println(sum); Q4. Design a class named Student that has two private data student id and score. The class should contain a parameterized constructor to initialize its data member and one method to display the information. Now write a Java program that will use an array of Student objects to represent information about 3 students. Your program should take input from the keyboard and display the information of the 3 students. 10 points import java.util.scanner; class Student { private int student_id; private int score; Student(int std_id, int s) { student_id = std_id; score = s; void display() { System.out.println("ID: " + student_id + ", score: " + score); public class Main { public static void main(string[] args) { Student students[] = new Student[3]; //Input Student information Scanner in = new Scanner(System.in); int stdid, stdscore;
for (int i = 0; i < 3; i++) { System.out.print("Enter student ID: "); stdid = in.nextint(); System.out.print("Enter score: "); stdscore = in.nextint(); students[i] = new Student(stdID, stdscore); //Display student information for (int i = 0; i < 3; i++) { students[i].display(); Q5. (a)why should you use the keyword super in your Java program? Explain with example. 4 points (b) Generate the output of the following program: 6 points class Add { protected int i; Add(int a) {i = a; protected void addit(int amount) {i += amount; protected int getit() {return i; class DAdd extends Add { private int i; DAdd(int a, int b) { super(a); i = b; protected void addit(int amount) {i = i * super.i + amount; protected int getit() {return i + 1; protected void doubleit(int amount) {addit(2 * amount); public class TestAdder { public static void main(string args[]) { Add A = new Add(3); DAdd DA = new DAdd(1, 5); A.addIt(2); System.out.println(A.getIt()); A = DA; A.addIt(2); System.out.println(A.getIt()); DA.doubleIt(2); System.out.println(A.getIt());
(a) The super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used. -- The super keyword as a standalone statement is used to call the constructor of the superclass in the base class. Example to use the super keyword to call the constructor of the superclass in the base class: public class Class1{ public Class1(String arg){ super(arg); (b) ) 5 8 12 Q6. Consider the following code, what is the output provided a valid file name is given on the command prompt? 10 points import java.io.*; class Q032{ public static void main(string args[]) throws Exception{ FileInputStream fin; int c = 0; try { fin = new FileInputStream(args[0]); while ((c = fin.read())!= -1) { System.out.print((char)c); catch (Exception e) { System.out.println(e); fin.close(); Compile-time error " Variable fin may not have been initialized."
Q7. What is the output? 5 points class A{ A() { System.out.print("1"); class Q005 extends A{ Q005() { System.out.print("2"); public static void main(string args[]) { Q005 obj = new Q005(); System.out.println("3"); 123 Q8. Predict the output: 5 points class Q008{ public static void main(string args[]){ int i = -1; System.out.println((i<0)?-i:i); 1
Q.9 Write a class named TestClass and add a String data field called data1. The data field should be private to the class. Now, add a constructor that accepts a starting value for data1 as its single parameter, and public methods for setting and retrieving the value of data1. Call these methods setdata() and getdata(). 5 points class TestClass { private String data1; TestClass(String val) { data1 = val; void setdata(string val) { data1 = val; String getdata() { return data1; Q10. If the variable num1 is equal to 5 and the variable num2 is equal to 10, how would you evaluate the logical expression ((num1!= 5) (num2 == 10)) &&!(num1 == 5)? Show each step of the evaluation. 5 points First Step: Second Step: Third Step: ((false true) &&!true) (true && false) false