COE 212 Engineering Programming. Welcome to Exam I Tuesday November 11, 2014

Similar documents
COE 211 Computer Programming. Welcome to Exam I Tuesday March 13, 2018

COE 212 Engineering Programming. Welcome to Exam I Thursday June 21, Instructor: Dr. Wissam F. Fawaz

COE 212 Engineering Programming. Welcome to Exam II Tuesday November 28, 2018

COE 212 Engineering Programming. Welcome to Exam II Thursday April 21, Instructors: Dr. Salim Haddad Dr. Joe Tekli Dr. Wissam F.

COE 212 Engineering Programming. Welcome to the Final Exam Tuesday December 15, 2015

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

COE 212 Engineering Programming. Welcome to Exam II Friday November 27, 2015

COE 211/COE 212 Computer/Engineering Programming. Welcome to Exam II Thursday December 20, 2012

COE 212 Engineering Programming. Welcome to Exam II Wednesday November 30, 2016

COE 212 Engineering Programming. Welcome to the Final Exam Thursday December 15, 2016

COE 212 Engineering Programming. Welcome to Exam II Friday April 20, Instructors: Dr. Dima El-khalil Dr. Wissam F. Fawaz

COE 212 Engineering Programming. Welcome to the Final Exam Monday May 18, 2015

I. True/False: (2 points each)

CS 102 / CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2010

H212 Introduction to Software Systems Honors

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Programming with Java

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2010

Using Classes and Objects Chapters 3 Creating Objects Section 3.1 The String Class Section 3.2 The Scanner Class Section 2.6

Mr. Monroe s Guide to Mastering Java Syntax

I. True/False: (2 points each) On your bubble form fill out a for true and b for false.

Array. Prepared By - Rifat Shahriyar

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Selected Questions from by Nageshwara Rao

H212 Introduction to Software Systems Honors

What methods does the String class provide for ignoring case sensitive situations?

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

AP Computer Science A

Introduction to Computer Science Unit 2. Notes

Oct Decision Structures cont d

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

Section 2.2 Your First Program in Java: Printing a Line of Text

Problem Grade Total

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

Using Java Classes Fall 2018 Margaret Reid-Miller

Introduction to Computer Science Unit 2. Exercises

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

Chapter 3. Selections

ICSE Class 10 Computer Applications ( Java ) 2014 Solved Question Paper

CS111: PROGRAMMING LANGUAGE II

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Lab5. Wooseok Kim

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

4 WORKING WITH DATA TYPES AND OPERATIONS

Object Oriented Programming. Java-Lecture 1

Full file at

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Chapter 2 ELEMENTARY PROGRAMMING

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

MIDTERM REVIEW. midterminformation.htm

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Conditional Programming

CEN 414 Java Programming

Topics. Chapter 5. Equality Operators

Computational Expression

CMSC131. Introduction to your Introduction to Java. Why Java?

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Fundamentals of Programming Data Types & Methods

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Classes and Objects Part 1

Lab5. Wooseok Kim

Chapter 4 Defining Classes I

2: Basics of Java Programming

double float char In a method: final typename variablename = expression ;

Question: Total Points: Score:

AP Computer Science Unit 1. Writing Programs Using BlueJ

Java Bytecode (binary file)

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

AP CS Unit 3: Control Structures Notes

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Chapter. Let's explore some other fundamental programming concepts

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

EECS168 Exam 3 Review

Starting Out with Java: From Control Structures through Data Structures 3e (Gaddis and Muganda) Chapter 2 Java Fundamentals

Table of Contents Date(s) Title/Topic Page #s. Chapter 4: Writing Classes 4.1 Objects Revisited

Computer Programming, I. Laboratory Manual. Final Exam Solution

DM550 Introduction to Programming part 2. Jan Baumbach.

CS121: Computer Programming I

This Week s Agenda (Part A) CS121: Computer Programming I. Changing Between Loops. Things to do in-between Classes. Answer. Combining Statements

Course Outline. Introduction to java

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

I. True/False: (2 points each)

A variable is a name for a location in memory A variable must be declared

Faculty of Science COMP-202A - Introduction to Computing I (Fall 2008) Midterm Examination

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Full file at

Elementary Programming

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Fundamentals of Programming Session 23

Introduction to Computer Science Unit 2. Notes

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Transcription:

1 COE 212 Engineering Programming Welcome to Exam I Tuesday November 11, 2014 Instructors: Dr. Bachir Habib Dr. George Sakr Dr. Joe Tekli Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book. Please do not forget to write your name and ID on the first page. 2. You have exactly 120 minutes to complete the 6 required problems. 3. Read each problem carefully. If something appears ambiguous, please write your assumptions. 4. Do not get bogged-down on any one problem, you will have to work fast to complete this exam. 5. Put your answers in the space provided only. No other spaces will be graded or even looked at. Good Luck!!

Problem 1: Multiple choice questions (20 minutes) [16 points] For each question, choose the single correct answer. 1) A Java object instantiation statement would contain: a. The new keyword b. The name of the object c. A constructor method d. All of the above 2) Calling a non-static method that belongs to another class requires which of the following items? a. An object created from the other class b. The dot operator (.) 3) What is the name of the values passed into a method during method invocation? a. Formal parameters b. Actual parameters c. Arguments 4) What type of methods allows a client program to assign values to the private instance variables of a helper class? a. Mutator methods b. Setter Methods 5) Consider the following Java statements: String str = ; double val = 12.5; Which of the following can be used to store the value of the variable called val in str? a. str += val; b. str = Double.parseDouble(Double.toString(val)) + ; 6) Which of the following expressions correctly computes 5 + 2? a. int result = 5 + Math.sqrt(Math.pow(2, 5)); b. int result = 5 + Math.pow(Math.pow(2, 5), 0.5); 7) Which of the following classes is part of the java.util package? a. DecimalFormat b. Integer c. System 8) Which of the following statements is an invalid expression in Java? a. int result = ( (int) (19.0+4) 5; b. int result = (14+9) * 5; c. int result = 10 % 2; 2

9) Which of the following methods can be used to arrange a set of String values in alphabetical order? a. equals b. equalsignorecase c. compareto 10) Which of the following data types allows only one of two possible values to be assigned to a variable? a. char b. bool c. int 11) Which of the following can be used to capitalize the first letter of a String called st? a. st= st.charat(0).touppercase()+st.substring(1,st.length()); b. st= st.substring(0,1).touppercase()+st.substring(1,st.length()); 12) Let b a variable of type float. The compilation of which of the below statements would result in a compile-time error? a. b++; b. b--; c. b = b + 1.5; 13) What output is produced by the following Java statement: System.out.print( //// ); a. /// b. // c. //// 14) Consider having two String variables str1 and str2. The statement str1.equalsignorecase(str2); can be achieved using: a. str1.tolowercase.equals(str2.tolowercase); b. str1.touppercase().equals(str2.touppercase()); 15) Which statement below can be used to simulate the process of selecting a card from a deck of 52 cards? Suppose rnd is a Random object. a. Math.abs(rnd.nextInt()%52) + 1; b. (int) rnd.nextfloat()*52 + 1; c. rnd.nextfloat(52); d. Both (a) and (b) 16) A variable whose scope is the entire class is known as a(n) a. parameter b. instance variable c. local variable 3

4 Problem 2: True or false questions (10 minutes) [10 points] 1. The output of the following statements is: false String str1 = Ofelia, str2 = new String( Ofel ); str2 = str2.concat(str1.substring(4, 5)); System.out.print(str1.equals(str2)); 2. The following code prints character r on-screen: String s = Constitution ; System.out.print(s.charAt(7)); 3. There are two different forms of comment statements in Java. 4. Given that a single quote ( ) is used to mark the beginning or the end of an output character, it is not possible to print the single quote character ( ) using a System.out.println statement. 5. Declaring a method with private visibility violates the encapsulation principle as all methods inside a class must be public. 6. A proper way to enable access to instance variables is to make those instance variables public. 7. The following assignment statement is a valid Java statement: Char string = c ; 8. After running the code shown below, the value stored in the variable y is 4 String str = Exam is fun ; int y = str.replace( E, e ).substring(0, 5).length(); 9. The output of the code shown below is: 8 DecimalFormat fmt1 = new DecimalFormat( 0.## ); DecimalFormat fmt2 = new DecimalFormat( 0.# ); double val = Double.parseDouble(fmt1.format(8.876)); System.out.print(fmt2.format(val).charAt(2)); 10. The output of the statement below is a random value between 1 (inclusive) and 6 (exclusive). System.out.print((int) Math.random() * 6 + 1);

Problem 3: Long true or false question (15 minutes) [14 points] In the following questions, check all the correct answers. There is at least one correct answer per question, but there may be more. 1. Which of the following are false: a. By convention, class names begin with an uppercase letter, but methods and variables begin with a lowercase letter. b. Instance variables can be declared anywhere inside a class. c. Instance variables exist before methods are called on objects, while the methods are executing and after the methods complete execution. 2. Which of the following are true: a. Each object of the class contains the class s instance variables. b. Most instance variable declarations are preceded with the keyword public, which is a visibility modifier. c. Variables or methods declared with private visibility are accessible only to the methods of the class in which they are declared. 3. Which of the following statements are false: a. The method s return type specifies the type of data returned to the calling method. b. No parentheses following a method name indicate that the method does not require any parameters to perform its task. c. Classes often provide private methods to allow the class s client to set or get public instance variables. 4. Which of the following are false: a. A return type of null indicates that a method will perform a task but will not return any information. b. A method s parameters are local variables of the method, in the sense that they can be used outside the method. c. Every method s body is delimited by a left and right braces { and. 5. Which of the following are false: a. Scanner method nextline() reads characters until a line separator is encountered, then returns the characters as a String. b. To call a method of an object, follow the method name with a dot, the class name and finally a set of parentheses containing the method s arguments. c. A constructor is a method that is called by the new operator to initialize an object s instance variables at the time the object is created. 6. Which of the following are true: a. Instance variables cannot be initialized inside a class s constructor. b. A String object can be assigned an initial value of null. c. The types of actual parameters must be identical to the types of formal parameters. 7. Which of the following are true: a. A primitive data type like double does not offer methods. b. A primitive-type variable does not store an address. c. A reference to an object is always required before invoking a class s methods. 8. Which of the following are true: a. If a class does not define constructors, the compiler generates a syntax error b. If you declare a constructor for a class, the compiler does not create a default constructor for that class. c. Constructor methods can specify parameters but not return types. 5

6 Problem 4: Code analysis (15 minutes) [10 points] 1) Consider the class given below, along with the driver class for it. public class ClassA { private int x, y; public ClassA() { x = 5; y = 2; public int first(int x){ int temp = y+1; y = x; this.x = temp; return x; public void second(int x, int y) { this.y = y+1 ; public int getx(){return x; public int gety() {return y; public class ClassADriver { public void doit() { ClassA a=new ClassA(); int x = a.first(2); a.second(2, x); x = a.getx(); int y = a.gety(); System.out.println( Answer is: + (x+y)); When running the method doit() of the ClassADriver class, what output is produced? a. Answer is: 2 b. Answer is: 4 c. Answer is: 6 d. It doesn t compile correctly e. None of the above 2) Consider the class given below, along with a driver class for it. public class ClassB { private int x; public ClassB() { x = 0; addvalue(x); private void addvalue(int val){ x = x+val; public int getx() { return x; public class ClassBDriver { public void doit() { int y = 6; ClassB b=new ClassB(); b.addvalue(y); System.out.println( value is: + b.getx()); When running the method doit() of the ClassBDriver class, what output is produced? a. value is: 4 b. value is: 6 c. value is: 8 d. It doesn t compile correctly e. None of the above

7 Problem 5: Evaluating Java Expressions (15 minutes) [10 points] For each of the following code fragments, what is the value of x after the statements are executed? (1) double u = 15.51; double v = Math.floor(u*10); int x = (int)(math.ceil(v) - u/100); Answer: x= 154 (2) int a=5, b=12; String str = "Good Morning Byblos"; String x = "\"+str.substring(a,b)+\""; Answer: x= + str.subtring(a,b) + (3) DecimalFormat fmt = new DecimalFormat("00.##"); double a = 3.456; double b = 10; b = a++; String x = fmt.format(b); x += 10; (2 pts) Answer: x= 03.4610 (4) String str = "Ignorance is ignorance itself"; str = str.replace('o', 'a'); String x = str.substring(str.indexof('a'), str.indexof('s')); Answer: x= arance i (5) Random rnd = new Random(); int y = rnd.nextint(16)%3; int x = (++y) * (int)rnd.nextfloat(); Answer: x= 0 (6) int u = 3, v = 20; double x = v++ / u-- + u++; x += v / u; (2 pts) Answer: x= 15.0 (7) String str = "Ladies and Gents"; char x = str.charat(str.length() - str.substring(5).length()); Answer: x= s

8 Problem 6: Coding Problems (45 minutes) [40 points] 1) Write a Java program that reads the (x1, y1) coordinates of a first point from the user, then randomly generates (x2, y2) coordinate values between 0 (inclusive) and 100 (exclusive) for a second point. Your program should finally compute and print out the distance between the two points (x1, y1) and (x2, y2) using the following formula: Distance = Sample output: First point (user-supplied values): Enter x1: 1 Enter y1: 0 Second point (randomly generated): x2 = 0 y2 = 0 Distance between two points = 1.0 import java.util.scanner; import java.util.random; import java.util.text; public class DistanceCalculation { public static void main (String[] args) { double x1, y1, x2, y2; double ManDist, EucDist; Scanner scan = new Scanner(System.in); System.out.println("First point (user-supplied values): "); System.out.print(" Enter x1: "); x1 = scan.nextdouble(); System.out.print(" Enter y1: "); y1 = scan.nextdouble(); Random rnd = new Random(); x2 = rnd.nextfloat() * 100; y2 = rnd.nextfloat() * 100; DecimalFormat fmt = new DecimalFormat( 0.## ); System.out.println("Second point (randomly generated): "); System.out.println("x2= " + fmt.format(x2)); System.out.println("y2= " + fmt.format(y2)); EucDist = Math.sqrt( Math.pow(x1-x2, 2) + Math.pow(y1-y2, 2) ); System.out.println ("Distance between two points = " + EucDist);

9 2) Write a Java program that reads two integer values from the user and then prints out the average of their respective unit digits (i.e., rightmost digits). Sample output: Enter val1: 16 Enter val2: 23 The average of 6 and 3 is 4.5 import java.util.scanner; public class UnitAverage { public static void main(string[] args) { int digit1, digit2; double average; int val1, val2; Scanner scan = new Scanner(System.in); System.out.print("Enter Val1:"); val1 = scan.nextint(); System.out.print("Enter Val1:"); val2 = scan.nextint(); digit1 = val1 % 10; digit2 = val2 % 10; average = (digit1 + digit2)/2.0; System.out.println("The average of " + digit1 + " and " + digit2 + " is " + average);

10 3) Write a Java program that reads a string from the user and then prints out a new string formed by the first three characters and the last 5 characters of the input string. We assume that the input string s length exceeds 8. Sample output: Enter a string: Android Programming Output string: Andmming import java.util.scanner; public class StringManip { public static void main(string[] args) { String S1, S2; Scanner scan = new Scanner(System.in); System.out.print("Enter a string:"); S1 = scan.nextline(); S2 = S1.substring(0, 3) + S1.substring(S1.length()-5, S1.length()); System.out.println("Output string:" + S2);

11 4) Write a Java program that reads two dates from the user, namely the current date and the user s date of birth. Your program should then compute the age of the user based on the input date values. Note that we assume that the date values entered by the user are formatted as follows: dd/mm/yyyy (as in 23/08/2014). Sample output: Enter the current date: 11/11/2014 Enter date of birth: 23/12/2009 You are 5 years old. import java.util.scanner; public class Age{ public static void main(string[] args) { String S1, S2; int year1, year2, age; Scanner scan = new Scanner(System.in); System.out.print("Enter the current date:"); S1 = scan.nextline(); System.out.print("Enter date of birth:"); S2 = scan.nextline(); S1 = S1.substring(S1.length()-4, S1.length()); S2 = S2.substring(S2.length()-4, S2.length()); System.out.print(S1 + S2); year1 = Integer.parseInt(S1); year2 = Integer.parseInt(S2); age = year1 - year2; System.out.println("You are " + age + " years old");