Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75



Similar documents
AP Computer Science Java Subset

JAVA ARRAY EXAMPLE PDF

Part I. Multiple Choice Questions (2 points each):

J a v a Quiz (Unit 3, Test 0 Practice)

Using Files as Input/Output in Java 5.0 Applications

Sample CSE8A midterm Multiple Choice (circle one)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Object-Oriented Programming in Java

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Introduction to Java

Chapter 2 Introduction to Java programming

Topic 11 Scanner object, conditional execution

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Install Java Development Kit (JDK) 1.8

AP Computer Science Static Methods, Strings, User Input

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

CS 121 Intro to Programming:Java - Lecture 11 Announcements

CS506 Web Design and Development Solved Online Quiz No. 01

Object-Oriented Programming in Java

java Features Version April 19, 2013 by Thorsten Kracht

CSE 8B Midterm Fall 2015

CS170 Lab 11 Abstract Data Types & Objects

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Introduction to Java. CS 3: Computer Programming in Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CS 111 Classes I 1. Software Organization View to this point:

Web Development and Core Java Lab Manual V th Semester

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Introduction to Programming

Building Java Programs

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

More on Objects and Classes

Inheritance, overloading and overriding

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

History OOP languages Year Language 1967 Simula Smalltalk

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Homework/Program #5 Solutions

Programming Languages CIS 443

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Java Interview Questions and Answers

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Building Java Programs

Introducing Variance into the Java Programming Language DRAFT

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

The following program is aiming to extract from a simple text file an analysis of the content such as:

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Building Java Programs

Java Programming Language

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

STATIC VARIABLE/ METHODS, INHERITANCE, INTERFACE AND COMMAND LINE ARGUMENTS

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Java Application Developer Certificate Program Competencies

INPUT AND OUTPUT STREAMS

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

D06 PROGRAMMING with JAVA

Introduction to Object-Oriented Programming

Software Development with UML and Java 2 SDJ I2, Spring 2010

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

AP Computer Science Java Mr. Clausen Program 9A, 9B

Java CPD (I) Frans Coenen Department of Computer Science

Konzepte objektorientierter Programmierung

Comp 248 Introduction to Programming

Basic Programming and PC Skills: Basic Programming and PC Skills:

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Java (12 Weeks) Introduction to Java Programming Language

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Files and input/output streams

Data Structures Lecture 1

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Fundamentals of Java Programming

Course Intro Instructor Intro Java Intro, Continued

Syllabus for CS 134 Java Programming

Chapter 3. Input and output. 3.1 The System class

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Basics of Java Programming Input and the Scanner class

CS193j, Stanford Handout #10 OOP 3

Smallest Java Package? Java.applet.* having 1 class and 3 interfaces. Applet Class and AppletContext, AppletStub, Audioclip interfaces.

Unit Testing. and. JUnit

INTRODUCTION TO COMPUTER PROGRAMMING. Richard Pierse. Class 7: Object-Oriented Programming. Introduction

Conditionals (with solutions)

public static void main(string[] args) { System.out.println("hello, world"); } }

Programmation 2. Introduction à la programmation Java

Transcription:

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