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

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

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

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

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

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

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 Thursday December 15, 2016

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

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

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

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

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

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

1 Short Answer (10 Points Each)

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CS141 Programming Assignment #6

CSC 1051 Data Structures and Algorithms I

Loops. CSE 114, Computer Science 1 Stony Brook University

I. True/False: (2 points each)

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Java I/O and Control Structures

Bjarne Stroustrup. creator of C++

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Problem Grade Total

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CSC 1051 Data Structures and Algorithms I

Java I/O and Control Structures Algorithms in everyday life

AP CS Unit 3: Control Structures Notes

CS111: PROGRAMMING LANGUAGE II

Iteration statements - Loops

Programming with Java

Section 004 Spring CS 170 Exam 1. Name (print): Instructions:

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Introduction to Computer Science Unit 2. Exercises

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

Repetition. Chapter 6

Repetition. Chapter 6

I. True/False: (2 points each)

Java Coding 3. Over & over again!

AP COMPUTER SCIENCE A

Selection and Repetition Revisited

Selection Statements and operators

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Faculty of Science COMP-202B - Foundations of Computing (Winter 2016) - All Sections Midterm Examination

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

Term 1 Unit 1 Week 1 Worksheet: Output Solution

Introduction to Computer Science Unit 2. Notes

Final Exam. COMP Summer I June 26, points

H212 Introduction to Software Systems Honors

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

Choose 3 of the 1 st 4 questions (#'s 1 through 4) to complete. Each question is worth 12 points.

Top-Down Program Development

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Full file at

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

CSCI 1226 Sample Midterm Exam

Last Class. While loops Infinite loops Loop counters Iterations

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Chapter 6. Repetition Statements. Animated Version The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Data Types and the while Statement

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

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

CMPS 12A Winter 2006 Prof. Scott A. Brandt Final Exam, March 21, Name:

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

Question: Total Points: Score:

Selection and Repetition Revisited

CS1004: Intro to CS in Java, Spring 2005

I. True/False: (2 points each)

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

CSE 1223: Exam II Autumn 2016

Introduction to Computer Science Unit 2. Notes

Question: Total Points: Score:

Loops / Repetition Statements

Question: Total Points: Score:

Section 003 Fall CS 170 Exam 1. Name (print): Instructions:

Module Contact: Dr Gavin Cawley, CMP Copyright of the University of East Anglia Version 1

Programming Constructs Overview. Method Call System.out.print( hello ); Method Parameters

Assignment 2.4: Loops

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

Chapter 3. Selections

SFWR ENG/COMP SCI 2S03 Principles of Programming SOLUTIONS

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

Question: Total Points: Score:

Methods and Functions

COE 211/COE 212 Computer Programming/Engineering Programming. Welcome to The Comprehensive Final Exam Monday June

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Selection Statements and operators

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

Repetition, Looping. While Loop

Faculty of Science COMP-202B - Introduction to Computing I (Winter 2009) Midterm Examination

Loops. GEEN163 Introduction to Computer Programming

Object Oriented Programming. Java-Lecture 1

Lecture 5: Methods CS2301

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Transcription:

1 COE 212 Engineering Programming Welcome to Exam II Thursday April 21, 2016 Instructors: Dr. Salim Haddad 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] Consider an input text file called in.txt that contains the following data: 2+4 3*5 6-5 Assume that a Scanner object called infromfile was instantiated properly to read data from in.txt 1) Which of the following statements creates a String called line1 that stores the first line of in.txt? a. String line1 = infromfile.next(); b. String line1 = infromfile.nextline(); 2) Given the line1 object created earlier, which of the following code evaluates the summation in line1 correctly? Assume that sum is an int that was properly declared and initialized to 0 a. for(int i=0; i<line1.length; i++) if(line1.charat(i)!= + ) sum+=integer.parseint(line1.charat(i)+ ); b. for(int i=0; i<line1.length; i++) if(line1.substring(i, i+1).equals( + )) sum+=integer.parseint(line1.substring(i, i+1)); 3) Assume that line2 is a String variable holding the second line of in.txt. Which of the following creates an object that can be used to obtain the numbers on either side of * in line2? a. Scanner line2scan = new Scanner(line2); linescan.setdelimiter( * ); b. Scanner line2scan = new Scanner(line2); linescan.setdelimiter( * ); c. Scanner line2scan = new Scanner(line2, * ); 4) Given the line2scan object created earlier. Which of the following evaluates the product in line2 correctly? Assume that prod is an int that was properly declared and initialized to 0 a. for(int i=1;i<=2;i++) prod*=integer.parseint(line2scan.next()); b. for(int i=0;i<=1;i++) prod*=integer.parseint(line2scan.next()); 5) line3 is a String variable holding the third line of in.txt. Which of the following creates 2 variables called num1 and num2 holding the numbers on either side of the operator in line3? a. int num1=integer.parseint(line3.charat(line3.indexof( - )-1)); int num2=integer.parseint(line3.charat(line3.indexof( - )+1)); b. int num1=integer.parseint(line3.substring(0,1)); int num2=integer.parseint(line3.substring(2,line3.length()); 6) Given the num1 and num2 variables created earlier. Which of the following correctly finds their greatest common divisor? a. while(num1!= num2) if(num1<num2) num1 = num1 num2; else num2 = num2 num1; b. while(num1!= num2) if(num1<num2) num1-=num2; if(num2<num1) num2-=num1; 2

7) Which of the following finds the larger value between num1 and num2 and stores it in an int variable called larger? a. larger = (num1>num2) : num1? num2; b. larger = (num1>num2) : num2? num1; c. if(num1 num2 > 0) larger = num1; else larger = num2; 8) When writing data to a file, which of the following import declaration statements is not needed? a. import java.io.ioexception; b. import java.io.file; c. import java.io.filereader; 9) Which of the following methods have been overloaded multiple times by the Java language? a. print b. nextint 10) Which of the following results in a compile-time error? a. Not placing throws IOException at the end of the header of a main method that writes data to an output file b. Not closing the PrintWriter object inside a main method that writes data to an output file 11) What type of relationship exists between String and Comparable? a. uses relationship with String implementing the methods of Comparable b. has a relationship with String containing references to Comparable objects c. aggregation relationship with String implementing the methods of Comparable 12) Which of the following cannot be placed inside the body of a conditional statement? a. break b. continue 13) Which of the following is not part of any Java built-in interface? a. compareto b. nextline c. hasnext 14) Which of the following is false about static variables? a. Each object has its own copy of a static variable with its own memory space b. A static variable cannot be referenced by name by a non-static method c. A static variable can be referenced by name by a static method d. All of the above 15) Which of the following is false about switch? a. The default case is optional b. The expression evaluated at the beginning of a switch statement can be of type byte c. Not including a break statement inside the first case results always in the execution of the statements corresponding to all the cases 16) In Java, which of the following runs forever? a. int val = 12; while(val < 13) val--; b. while(true) continue; 3

Problem 2: True or false questions (20 minutes) [14 points] 1. Assuming that x, y and z are int variables, the following code fragment: if(!(x<y) (y>z)) System.out.print( Met ); else System.out.print( Not met ); can be rewritten as: if(x>=y && y<=z) System.out.print( Not met ); else System.out.print( Met ); 2. The output of the program segment below is: 1 int x=2, y=2, z=3, w=1, s; if(x < y) if(x < z) s = x ; else s = z; else if(y < z) s = y; else s = z; if(w < s) s = w; System.out.print(s); 3. The following method returns true if x is prime and false otherwise. Assume that x is strictly greater than 1. public boolean method1(int x) int y=0; for(int i=2; i<x/2; i++) if(x%i==0) y++; return (y==0); 4. Assume that the method1 given earlier can be used to determine whether or not its parameter is prime. The following method prints the largest prime number that is less than or equal to x. Assume that x is strictly greater than 1. public void method2(int x) for(int y=x; y>=2; y--) if(method1(y)) System.out.print(y); break; 5. The body of the following repetition statement executes 5 times: int count = 1; while(true) count++; if(count == 6) break; 6. Consider the following nested while loops. The body of the inner while loop executes 100 times. int count1=1, count2; while(count1<=10) count2 = 0; while(count2 <= 10) System.out.println(count2); count2++; count1++; 7. In Java, overloading makes it possible for two different classes like Scanner and Random to both have a method called nextint(). 4

8. The following method returns the middle digit of the input parameter x. Assume that x has an odd number of digits. public int method2(int x) int y; String z = x + ; y = Integer.parseInt( z.substring(x.length()/2, x.length()/2+1)); return y; 9. The following code prints the even numbers between 1 (inclusive) and 10 (inclusive). int count=2; do if(count % 2!= 0) count++; continue; System.out.println(count); count++; while(count <= 10); 10. The following code prints Tails when x is 0 and Heads when x is 1. int x = (int) (Math.random() * 2); switch(x) case 0: System.out.print( Tails ); case 1: System.out.print( Heads ); 11. The following code prints: 2nd threshold reached int val=2; if(val == 3) System.out.println( 1st threshold reached ); else if(val == 2) System.out.println( 2nd threshold reached ); System.out.println( Neither thresholds reached ); 12. The following code prints: 4 int x = 1; for(int i=0; i<3; i++) for(int j=i+1; j<3; j++) x = x*j; System.out.print(x); 13. A local variable is created when a method is called and then destroyed when the method is exited. An instance variable is created when the class containing it is instantiated and then destroyed when the program using that instance of the class terminates its execution. 14. The following code uses two overloaded versions of the nextint method. Random rnd = new Random(); System.out.println(rnd.nextInt()); System.out.println(rnd.nextInt(3)); 5

6 Problem 3: Code analysis (10 minutes) [10 points] 1) Consider the class given below. What output is produced when it is executed? public class ClassA public static void main(string[] args) String name1= Salim, name2= Joe ; method1(name1, name2); System.out.print( Name1: +name1+, Name2: +name2); private static void method1(string name1, String name2) String temp; temp = name1; name1 = name2; name2 = temp; a. Name1: Salim, Name2: Joe b. Name1: Joe, Name2: Salim c. Name1: Joe, Name2: Joe 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(int x) this.x=x; public int getx() return x; public void setx(int x) this.x=x; public String tostring() return Integer.toString(x); When running ClassBDriver class, what output is produced? a. b1:2, b2:3 b. b1:3, b1:2 c. It produces a run-time error d. It doesn t compile correctly e. None of the above public class ClassBDriver public static void main(string[] args) ClassB b1 = new ClassB(2); ClassB b2 = new ClassB(3); method2(b1, b2); System.out.print( b1: +b1+, b2: +b2); private void method2(classb b1, ClassB b2) int temp; temp = b1.getx(); b1.setx(b2.getx()); b2.setx(temp);

7 Problem 4: Code analysis (15 minutes) [10 points] For each of the following code fragments, what is the value of x after the statements are executed? (1) char S= + ; int x = 20; switch(s) case - : x -= 3; break; case + : x += 2; case * : x /= 4; break; case / : x *= 4; default: x+=10; Answer: x = 5 (2) int n=10, x=0; while(x<n) x += 5; for(int j=0; j<x; j+=3) n= n+2; x=n; Answer: x= 18 (3) int x=2, i=7; do i++; x -= i; i*=3; while(i < 20); x -= i; Answer: x= -30 (4) boolean x=true; for(int i=0; i<=3; i++) if(i/3 == 0) x =!((i > 0) && x); Answer: x= true (5) String S = "5 a 4 b 3 c 2 d 1"; int x = 1, n; Scanner scan = new Scanner(S); while(scan.hasnextint()) n = scan.nextint(); if(n > x+1)x = n; Answer x = 5

8 (6) String S = new String("Jim ate the fig"); int y = 0; char c; String x = ""; Scanner scan = new Scanner(S); while (scan.hasnext()) x += scan.next().substring(scan.next().length()-1); Answer: x = me (7) int x=8, s; for(s=--x; s>0; s -=2) x -= s; s--; Answer: x = -9 (8) String x = ""; int price = 80; if(price >= 90 price <=50) if(price <= 50) if (price >= 40) x = "average price"; else x = "cheap"; else x = "expensive"; Answer: x = (9) int x = 1; for(int i=0; i<3; i++) for(int j=i; j<3; j++) x = x+j; Answer: x = 9 (10) String x=""; int y = 1; for(;y<= 5; y+=1) if(y % 2 == 0) continue; x += y; y++; Answer: x = 135

9 Problem 5: Method definition (15 minutes) [10 points] You are given below information about 3 methods called sumevenrange, minthree, and countx. Your job is to complete the definition of each one of these methods as per the provided guidelines. 1. sumevenrange() is a method that accepts two integers a and b as input parameters, and returns the sum of the even integers in that range. public static int sumevenrange(int a, int b) int sum = 0; for(int i = a; i<=b; i++) if(i%2 == 0) sum += i; return sum; 2. minthree() is a method that accepts three double numbers x, y, and z as input parameters, and returns the minimum of the three. public static double minthree(double x, double y, double z) double min; if (x < y) if (x < z) min = x; else min = z; else if (y < z) min = y; else min = z; return min; 3. countx() is a method that accepts a String input parameter S, and returns the number of times character x appears in S, considering both its uppercase and lowercase forms (counting the occurrences of both: x and X ). public static int countx(string S) int count = 0; S = S.toLowerCase(); for(int i=0; i< S.length(); i++) if(s.charat(i) == 'x') count++; return count;

10 Problem 6: Coding (40 minutes) [40 points] 1. Write a program called ReverseSentence which accepts as input a sentence consisting of a string S1 of words, and then produces another string S2 consisting of the words of S1 in reverse order. Sample output: Enter sentence: Those who dare fail achieve Reverse sentence: achieve fail dare who those import java.util.scanner; public class ReverseSentence public static void main(string [] args) Scanner scan = new Scanner(System.in); System.out.print("Enter sentence: "); String S1 = scan.nextline(); String S2 = ""; Scanner scans1 = new Scanner(S1); while(scans1.hasnext()) S2 = scans1.next() + " " + S2; System.out.print("Reverse sentence: " + S2);

11 2. Write a program called PowerOf2 that reads a positive integer number n as input, and then determines whether or not n is a power of 2 (for instance, numbers 1, 2, 4, and 8 are powers of 2). The program needs to perform the necessary user input validation in order to make sure that the number n provided as input is positive. Sample output: Enter a positive number: -3 Wrong input! Try again: 16 Number 16 is a power of 2 import java.util.scanner; public class PowerOf2 public static void main(string [] args) Scanner scan = new Scanner(System.in); System.out.print("Enter a positive integer: "); int n = scan.nextint(); int a = n; while (n <0) System.out.print("Wrong input! Try again: "); n = scan.nextint(); while(n/2 > 1 && n%2 == 0) n = n/2; if( n == 2) System.out.print("Number " + a + " is a power of 2"); else System.out.print("Number " + a + " is NOT a power of 2");

12 3. Write a program called PrintingStars that reads a positive integer number n as input, and then prints the following star (asterisk) configuration on-screen. Sample output: Enter a positive number: 4 Output configuration: * ** *** **** *** ** * import java.util.scanner; public class PrintingStars public static void main(string [] args) Scanner scan = new Scanner(System.in); System.out.print("Enter a positive number: "); int n = scan.nextint(); while(n <=0) System.out.print("Wrong input! Try again: "); n = scan.nextint(); System.out.println( Output configuration: ); for(int i=0; i< 2*n; i++) if(i<=n) for(int j=0; (j<i); j++) System.out.print("*"); else for(int j= 2*n-i; j>0; j--) System.out.print("*"); System.out.println();

13 4. Write a program called MathExpress that evaluates basic mathematical expressions such as 2+15.1 and 3.1416*4.5 provided as input by the user. The program accepts as input a string expression consisting of a number, followed by an arithmetic operator, followed by another number, such as 2+15.1. (operators that are allowed are: +, -, *, and /). Then the program scans the expression in order to compute and print its value on-screen. Sample output: Enter math expression: 2+15.1 Result: 17.1 import java.util.scanner; public class MathExpress public static void main(string [] args) Scanner scan = new Scanner(System.in); System.out.print("Enter math expression: "); String S = scan.nextline(); double n1, n2, result; char op; String operators = new String("+-*/"); int i = 0; while (operators.indexof(s.charat(i))<0 && i<s.length()) i++; n1 = Double.parseDouble(S.substring(0, i)); op = S.charAt(i); n2 = Double.parseDouble(S.substring(i+1)); if(op == '+') result = n1 + n2; else if(op == '-') result = n1 - n2; else if(op == '*') result = n1 * n2; else result = n1 / n2; System.out.print("Result: " + result);