Part 1. Multiple Choice Questions 1. Which of the following contains instructions on how to initialize objects of a class? a. method c.

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

Java Interview Questions and Answers

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

CS506 Web Design and Development Solved Online Quiz No. 01

Fundamentals of Java Programming

AP Computer Science Java Subset

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

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

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

Using Files as Input/Output in Java 5.0 Applications

Introduction to Java

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

Chapter 2 Introduction to Java programming

INPUT AND OUTPUT STREAMS

Creating a Simple, Multithreaded Chat System with Java

Object-Oriented Programming in Java

Java Programming Fundamentals

Install Java Development Kit (JDK) 1.8

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

The Interface Concept

Java CPD (I) Frans Coenen Department of Computer Science

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

java Features Version April 19, 2013 by Thorsten Kracht

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

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

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

Introduction to Programming

History OOP languages Year Language 1967 Simula Smalltalk

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

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

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

Introduction to Java. CS 3: Computer Programming in Java

Specialized Programme on Web Application Development using Open Source Tools

Java Application Developer Certificate Program Competencies

Event-Driven Programming

13 File Output and Input

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

More on Objects and Classes

Java from a C perspective. Plan

Crash Course in Java

Basics of Java Programming Input and the Scanner class

An Overview of Java. overview-1

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

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

Lecture J - Exceptions

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

Java SE 7 Programming

Java SE 7 Programming

Software Development in Java

Programmation 2. Introduction à la programmation Java

Files and input/output streams

Course Intro Instructor Intro Java Intro, Continued

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

Object-Oriented Programming: Polymorphism

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

The C Programming Language course syllabus associate level

Java SE 7 Programming

Java SE 8 Programming

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

Building Java Programs

Programming Languages CIS 443

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

Specialized Programme on Web Application Development using Open Source Tools

Sample CSE8A midterm Multiple Choice (circle one)

D06 PROGRAMMING with JAVA

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

Lesson: All About Sockets

Chapter 1 Java Program Design and Development

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Java Programming Language

Chapter 3. Input and output. 3.1 The System class

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

D06 PROGRAMMING with JAVA

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

Stream Classes and File I/O

DIPLOMADO DE JAVA - OCA

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

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

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

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

Chapter 13 - Inheritance

JAVA ARRAY EXAMPLE PDF

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

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

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

6.1. Example: A Tip Calculator 6-1

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Java EE Web Development Course Program

LAB4 Making Classes and Objects

C++ INTERVIEW QUESTIONS

Classes and Objects. Agenda. Quiz 7/1/2008. The Background of the Object-Oriented Approach. Class. Object. Package and import

Object-Oriented Programming Lecture 2: Classes and Objects

Construction of classes with classes

Konzepte objektorientierter Programmierung

Web Development and Core Java Lab Manual V th Semester

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Computing Concepts with Java Essentials

Transcription:

Part 1. Multiple Choice Questions (15 marks) 1. Which of the following contains instructions on how to initialize objects of a class? a. method c. Constructor b. function d. Module 2. Which of the following is used to for linking a Java class to an interface? a. Extends c. Throws b. inherits d. Implements 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 type 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 noofsahpes 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