F I N A L E X A M I N A T I O N

Similar documents
M257 Putting Java to work

Full file at Chapter 2 - Inheritance and Exception Handling

Midterm assessment - MAKEUP Fall 2010

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

PART1: Choose the correct answer and write it on the answer sheet:

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

First Name: AITI 2004: Exam 2 July 19, 2004

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

1 Shyam sir JAVA Notes

More on Objects in JAVA TM

Java Object Oriented Design. CSC207 Fall 2014

Class, Variable, Constructor, Object, Method Questions

Programming II (CS300)

Programming II (CS300)

Use the scantron sheet to enter the answer to questions (pages 1-6)

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Inheritance and Polymorphism

Introduction Unit 4: Input, output and exceptions

6.Introducing Classes 9. Exceptions

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2013

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2014

Inheritance (Part 5) Odds and ends

Questions Answer Key Questions Answer Key Questions Answer Key

Declarations and Access Control SCJP tips

CH. 2 OBJECT-ORIENTED PROGRAMMING

CS1150 Principles of Computer Science Objects and Classes

First Name: AITI 2004: Exam 2 July 19, 2004

CS 113 PRACTICE FINAL

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

BBM 102 Introduction to Programming II Spring Exceptions

Exception-Handling Overview

CMSC 331 Second Midterm Exam

Object Oriented Programming. Java-Lecture 11 Polymorphism

Computer Science II (20073) Week 1: Review and Inheritance

ITI Introduction to Computing II

Introduction to Programming Using Java (98-388)

Inheritance and Interfaces

MSc/ICY Software Workshop Exception Handling, Assertions Scanner, Patterns File Input/Output

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

S.E. Sem. III [CMPN] Object Oriented Programming Methodology

Programming overview

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

ITI Introduction to Computing II

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Final Exam Practice Questions

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Inheritance and Polymorphism

Making New instances of Classes

CSCI 136 Written Exam #0 Fundamentals of Computer Science II Spring 2015

Exam Duration: 2hrs and 30min Software Design

Inheritance Motivation

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Islamic University of Gaza Faculty of Engineering Computer Engineering Department

OVERRIDING. 7/11/2015 Budditha Hettige 82

IT101. File Input and Output

15CS45 : OBJECT ORIENTED CONCEPTS

Questions Answer Key Questions Answer Key Questions Answer Key

CMSC 132: Object-Oriented Programming II

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Example Program. public class ComputeArea {

OBJECT ORİENTATİON ENCAPSULATİON

Unit5: Packages and abstraction. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Practice Questions for Final Exam: Advanced Java Concepts + Additional Questions from Earlier Parts of the Course

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

CS 162, Lecture 25: Exam II Review. 30 May 2018

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Java Training JAVA. Introduction of Java

CS-202 Introduction to Object Oriented Programming

CSCE3193: Programming Paradigms

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

CS506 Web Design & Development Final Term Solved MCQs with Reference

Midterm assessment Spring The Solution

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2015

Topic 7: Algebraic Data Types

Java Fundamentals (II)

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

OBJECT ORIENTED PROGRAMMING TYm. Allotted : 3 Hours Full Marks: 70

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Chapter 5 Object-Oriented Programming

CSCI 355 Lab #2 Spring 2007

CIS 110: Introduction to computer programming

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

Introduction to Computing II (ITI 1121) Final Examination

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

1. An operation in which an overall value is computed incrementally, often using a loop.

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

5/23/2015. Core Java Syllabus. VikRam ShaRma

Transcription:

Faculty Of Computer Studies M257 Putting Java to Work F I N A L E X A M I N A T I O N Number of Exam Pages: (including this cover sheet( Spring 2011 April 4, 2011 ( 5 ) Time Allowed: ( 1.5 ) Hours Student Name: Student ID: Section No.: Instructions: 1. This exam has 3 parts and ALL questions must be answered. 2. This exam consists of 5 pages. 3. Student handbooks are NOT permitted in this examination. 4. The use of electronic devices that could have a memory is NOT permitted. 5. At the end of the examination, check that you have written your student ID, name and your section number on the first page. 1

Part 1. Multiple Choice Questions 1. Which of the following contains instructions to initialize objects of a class? a. method c. Constructor b. function d. Module 2. Which of the following is used for linking a Java class to an interface? a. Extends c. Throws b. inherits d. Implements (15 marks) 3. Which of the following is used to refer to the parent class of the class in which it appears? a. final c. super b. this d. static 4. One of the following is included in Java SDK: a. document generator c. NetBeans b. Java IDE d. All of the above 5. Which of the following is used to define a data field that is shared by all instances of a class? a. public c. static b. final d. Protected 6. Which of the following is the concept that Java relies on for providing its input/output facilities? a. Import c. Variables b. Packages d. Streams 7. Which of the following happens when subclasses modify the methods they inherit from a superclass. a. Inheritance c. Extension b. Overriding d. Overloading 8. To which of the following Java packages does the class HashMap belong? a. java.lang c. java.swing b. java.io d. java.util 9. Which of the following is used for defining groups of classes related by inheritance? a. packages c. abstract classes b. interfaces d. None of the above 10. Which of these data types requires the most amount of memory? a. double c. short b. float d. int 2

Part2. Essay: Answer the following questions. (30 marks) 1. Briefly explain the meaning of the following characteristics of Java language: - Threaded. - Dynamic. 2. Briefly describe the concepts of objects and classes in java. 3. Briefly describe the difference between primitive and reference variables. 4. Briefly describe the concepts of overriding and overloading in Java. 5. List one difference between checked exception and unchecked ones. 6. Name clash happens in Java when importing two packages to a Java program, and these packages have two classes with the same name. Briefly explain two methods to solve a name clash problem. Give examples. 3

Part 3. Problem Solving: Answer the following questions (55 marks) 1. Develop 3 classes Shape, Circle, and Rectangle according to the following specifications: (20 marks) The class hierarchy: The Shape class has: a public instance variables, String color. a private class variable noofshapes to store the number of created Shape objects. a one-argument constructor that sets the values of color to a given value, and increments noofshapes by 1. a static method getnoofshapes() that returns the number of created Shape objects. The Rectangle class has: two public instance variables int height and int width. a three-argument constructor that sets the values of color, height and width to given values. The Circle class has: a public instance variable, int radius. a two-argument constructor that sets the values of color and radius to given values. a public method tostring() that returns a string consisting of the circle s color and radius; for example, Color: Red, Radius: 10. 2. Re-write the following code using StringBuffer instead of String, and for instead of while: (10 marks) String[] names = new String[5]; int i = 0; while(i < names.length){ names[i] = ""; i++; 4

3. Write Java code to copy the first line of text from the file c:\input.txt and to the file c:\output.txt. Use the BufferedReader class to read the input, and the PrintWriter class to write the output. (10 marks) 4. Consider the following code and then answer the questions about it. (15 marks) public class CarChoice { public static void main(string[] args) { Scanner in = new Scanner(System.in); HashMap<Integer, String> carlist; carlist = new HashMap<Integer, String>(); carlist.put(1, "Toyota"); carlist.put(2, "Ford"); carlist.put(3, "Honda"); for (Integer idx : carlist.keyset()) System.out.println(idx+"-"+carList.get(idx)); System.out.println("Which brand would you prefer?"+ "(enter a number)"); try{ int br = in.nextint(); if (carlist.containskey(br)) System.out.println(carList.get(br) + " is a nice brand."); else System.out.println("Wrong brand number!"); catch(inputmismatchexception e){ System.out.println("Error! Not a number!"); (a) What will be displayed on the screen once the code is executed? (b) What will be the output if the user enters the following values: i - 1 ii - 4 iii - e Note: The method containskey()in the code above returns true if the HashMap contains a mapping for the specified key, and false otherwise. (c) Give an example from the code about the use of defensive programming, and another example about exception handling. 5

MULTIPLE CHOICE 1. ANS: C PTS: 2 KEY: Unit 1 & 3 MSC: MTA - Spring 2011 2. ANS: D 3. ANS: C 4. ANS: A 5. ANS: C 6. ANS: D 7. ANS: B 8. ANS: D 9. ANS: C 10. ANS: A ESSAY 1. ANS: Threaded: allow a program to do several things at once. Dynamic: Java programs can adapt to changes in their environment even while the program is running. 2. ANS: Objects in Java are defined by means of classes. A class is much like a template that defines the structure of: 1.the stored data associated with an object and 2.the program code that is executed when particular methods are invoked. 3. ANS: 1.Primitive variables store data values. e.g. int, float, etc 2.Reference variables do not themselves store data values, but are used to refer to objects. e.g. String. 4. ANS: * Overriding means that subclasses modifies the methods they inherit from a superclass. The method signature must stay the same. * Overloading means that a class can have more than one method of the same name, but with different signature. Overloading is not directly related to inheritance 5. ANS: Checked exceptions: - Programmers MUST include code to declare or handle any checked exceptions that might occur. - The Java compiler will report an error if this has not happened. Unchecked exceptions: - These exceptions need not be caught or declared. - Java contains facilities for catching the exceptions that can occur during the running of a program. They are normally due to programming errors. 6. ANS: Example of a name class: import java.util.*; import java.sql.*;... Date d1 = new Date(DATE_IN); //Date is ambiguous both libraries have a class with the name Date and thus we get an ambiguous name. Solution (1): to use the fully qualified name of the Date class (i.e. Method1 above) java.sql.date d1 = new java.sql.date(date_in); 6

Solution (2): to import only those classes you actually use, for example: import java.util.arraylist; import java.sql.* PROBLEM 1. ANS: public class Shape { public String color; private static int noofshapes; public Shape(String clr) { color = clr; noofshapes++; public static int getnoofshapes() { return noofshapes; public class Rectangle extends Shape{ public int height, width; public Rectangle(String clr, int h, int w) { super(clr); height = h; width = w; public class Circle extends Shape{ public int radius; public Circle(String clr, int rad) { super(clr); radius = rad; public String tostring(){ return "Color: " + color + ", Radius: " + radius; 2. ANS: StringBuffer[] names = new StringBuffer[5]; for(int i = 0; i < names.length; i++){ names[i] = new StringBuffer(""); 3. ANS: BufferedReader in = new BufferedReader(new FileReader("c:/input.txt")); PrintWriter out = new PrintWriter(new File("c:/output.txt")); out.write(in.readline()); out.close(); in.close(); 7

4. ANS: (a) 1 - Toyota 2 - Ford 3 - Honda Which brand would you prefer? (enter a number) (b) i) Toyota is a nice brand. ii) Wrong brand number! iii) Error! This is not a number! (c) Example of defensive programming: if..else.. statement is used to check that the use entered a correct number. Example of exception handling: try..catch.. statement is used to check that the user entered numeric value. 8