Docsity

Engineering and Programming Exam 2 - Computer Programming Lab | COE 211, Exams of Engineering

Toggle sidebar
bg1
1
COE 212 – Engineering Programming
Welcome to Exam II
Friday November 27, 2015
Instructors: Dr. Salim Haddad
Dr. Bachir Habib
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!!
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Engineering and Programming Exam 2 - Computer Programming Lab | COE 211 and more Exams Engineering in PDF only on Docsity!

COE 212 – Engineering Programming

Welcome to Exam II

Friday November 27, 2015

Instructors: Dr. Salim Haddad

Dr. Bachir Habib

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 ( 15 minutes) [14 points]

For the questions given below, consider the following statement:

String str = “www.someSchool.com/someDept/someFile.jpg”;

  1. Which of the following statements creates a Scanner object called scan that can be used to parse

the tokens separated by forward slash (/) characters in str?

a. Scanner scan = new Scanner(str, “/”); b. Scanner scan = new Scanner(str); scan.usesDelimiters(“/”); c. Scanner scan = new Scanner(str); scan.usesDelimiter(“/”); d. None of the above

  1. Given the scan object created earlier, what output does the following statement produce? System.out.print(scan.next() + “ ” + scan.hasNext()); a. www.someSchool.com/ true b. www.someSchool.com false c. www.someSchool.com/someDept true d. None of the above
  2. Which of the following statements produces the same output as the following code fragment? while(scan.hasNext()) System.out.println(scan.next()); Assume that these statements are executed immediately after the creation of the scan object given earlier. a. while(str.indexOf(‘/’) != -1) { System.out.println(str.substring(0, str.indexOf(‘/’))); str = str.substring(str.indexOf(‘/’)+1);} b. while(str.indexOf(‘/’) >= 0) { System.out.println(str.substring(0, str.indexOf(‘/’))); str = str.substring(str.indexOf(‘/’)+1, str.length()-1);} c. Both of the above d. None of the above
  3. Which of the following correctly counts the number of dots (.) characters present in str? a. int counter = 0; for(int i=0; i<=str.length(); i++) if(str.charAt(i) == ‘.’) counter++; b. int counter = 0; for(int i=0; i<str.length(); i++) if(str.charAt(i).equals(“.”)) counter++; c. int counter = 0; for(int i=0; i<str.length(); i++){ if(str.charAt(i)== ‘.’) counter++;} d. None of the above
  4. Which of the following statements prints on the same line the capital letters present in str in reverse order (backward)? a. for(int i=str.length()-1;i>=0;i++) if(str.charAt(i) >= ‘A’ && str.charAt(i) <= ‘Z’) System.out.print(str.charAt(i)); b. for(int i=str.length()-1;i>=0;i--) if(str.charAt(i) >= ‘A’ || str.charAt(i) <= ‘Z’) System.out.print(str.charAt(i)); c. for(int i=str.length()-1;i>=0;i--) if(str.charAt(i) >= ‘A’ && str.charAt(i) <= ‘Z’) System.out.print(str.charAt(i)); d. None of the above
  5. What output does the following code fragment produce? int counter=0; for(int i=0; i<str.length();i++) { switch(str.charAt(i)) { case ‘a’: counter++; break; case ‘e’: counter++; break;

Problem 2: True or false questions ( 15 minutes) [16 points]

  1. The following method correctly returns the maximum of its two input parameters num1 and num2. public int method1(int num1, int num2) { if(num1 > num2) return num1;} Answer: True False
  2. The following method correctly finds and returns the minimum of its three input parameters. public int method2(int num1, int num2, int num3) { int min; if(num1<num2) if(num1<num3)min=num1; else min = num3; else if(num2 < num3) min = num2; else min = num3; return min;} Answer: True False
  3. Non-static methods cannot reference static variables since non-static methods do not operate in the context of a particular object. Answer: True False
  4. Consider the following static method that returns the sum of its two input parameters: public static int method3(int num1, int num2) { return num1 + num2;} The following method correctly uses method3 to return the sum of its three input parameters: public int method4(int num1, int num2, int num3) { return method3(num1, method3(num2, num3));} Assume that method3 and method4 are part of the same class. Answer: True False
  5. The following code fragment outputs: 11 int z=1; for(;z<=10;) z++; System.out.print(z); Answer: True False
  6. The following code fragment correctly computes the sum of the values from 1 (inclusive) to 100 (inclusive): int sum; for(int i=1; i<=100; i++) { sum=0; sum+=i;} Answer: True False
  7. The following code fragment correctly computes the product of the square root of the values from 1 (inclusive) to 100 (inclusive): int prod = 1; for(int i=1; i<=100; i++) prod *= Math.sqrt(i); Answer: True False
  8. The following code fragment correctly computes the sum of the digits contained in the int variable called val. int sum=0; while(val != 0) sum = sum + val % 10; val = val/10; Answer: True False
  9. Consider the following nested loops: for(int i=1; i<=10; i++) { int j=0; while(true){if(j==10) break; j++;} } The body of the inner while loop is executed 100 times. Answer: True False
  1. The following Java code fragment fails to swap the values of val1 and val2. public static void main(String[] args) { int val1=1, val2=2; swap(val1, val2); System.out.println(“Val1:”+val1+“, val2:”+val2); } private static void swap(int num1, int num2) { int temp=num1; num1=num2; num2=temp; } Answer: True False
  2. The following code fragment generates two random numbers num1 and num2 between 1 (inclusive)

and 10 (inclusive) that are different from each other. int num1 = (int) (Math.random()10+1); do { num2 = (int) (Math.random()10 + 1); } while(num1 == num2); Answer: True False

  1. The following statement prints 1 star when rating is equal to 1 and the value of rating followed by a white space character and then the word stars otherwise. System.out.println(rating + ((rating==1)? “ stars”:“ star”)); Answer: True False
  2. The following code fragment outputs 3 rows each of which consisting of 3 back to back asterisk () characters. for(int i=1; i<=3; i++) for(int j=1; j<=3; j++) System.out.print(“”); Answer: True False
  3. The following code fragment uses multiple overloaded versions of the nextInt method of the Random class. Random rnd = new Random(); int x = rnd.nextInt(); int y = rnd.nextInt(6) + 1; Answer: True False
  4. The following code prints the lowest common multiple for the two int variables num1 and num2. int lcm = num1; while(true) { if(lcm % num2 == 0) break; lcm += num1; } System.out.println(lcm); Answer: True False
  5. PI is a static method of the Math class. Answer: True False

Problem 4: 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) int x=1, i=5;

do {

++i; x += i;

i*=2;

} while(i < 20);

x -= i;

Answer: x = -

(2) int answer = 2; int x = 10;

switch(answer) {

case 3:

x /= 2;

case 2:

x -= 2;

case 1:

x *= 2;

break;

case 0:

x /= 2;

break;

default:

x =2;

Answer: x = 16

(3) boolean x=false;

for(int i=-6; i<1; i++)

if(i/2 == 0)

x = (((2-i) < 0) && x)&& true;

Answer: x = false

(4) int x=1, v;

for(v=x; v<10; v+=2) x +=v;

x--;

Answer: x = 25

(5) int n=600; int x=n, i=1;

while(x>2) {

x=x/2; i++;}

for(int j=1; j<i; j +=2)

n= n/2;

x=n;

Answer: x = 37

(6) int y = 90;

boolean x =(y%2 == 0 && y / 10 != 0) && (y / 5 == 0);

Answer: x = false

(7) int x = 1;

for(int i=2; i <= 6; i++)

for(int j=3; j <= i; j++){

x += j;

j++;

Answer x = 23

(8) String S = "Bond! James Bond!", x = "";

for(int i=1; i < S.length(); i+=3)

x += S.charAt(i);

Answer x = “o!aso!”

(9) int z=0, w= 123, x=0;

do {

z = w%10;

if(z>x)

x=z;

w/=10;

} while(w > 3);

Answer: x = 3

(10) int x=0, s;

for(s=++x; s<=3; ++s)

x+=s;

s++;

Answer: x = 7

private static…………………………………… inputAge(…………………………){

} // end of inputAge method

private static…………………………………… checkEligibility(……………………){

} // end of checkEligibility method

} // end of class Voting

import java.util.Scanner;

class Voting{

public static void main (String [] args){

int age = inputAge();

if(checkEligibility(age) == true)

System.out.println("Eligible to vote");

else System.out.println("Not eligible to vote");

public static int inputAge(){

Scanner scan = new Scanner(System.in);

System.out.print("Enter your age:");

int age = scan.nextInt();

while(age <0 || age >130)

System.out.print("Wrong input! Try again:");

age = scan.nextInt();

return age;

public static boolean checkEligibility(int age){

boolean eligible = false;

if(age >= 18)

eligible = true;

return eligible;

2. Write a Java program called CountingCharacters that reads three strings from

the end user, then determines and prints how many times the letter ‘e’ appears in

the first string, the letter ‘t’ appears in the second string, and the letter ‘n’ appears

in the third string.

import java.util.Scanner;

public class CountingCharacters {

public static void main (String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter three string: ");

String S1 = scan.nextLine(); String S2 = scan.nextLine(); String S3 = scan.nextLine();

int eCount = 0; int tCount = 0; int nCount = 0;

for (int i=0; i<S1.length(); i++) if (S1.charAt(i) == 'e') eCount++;

for (int i=0; i<S2.length(); i++) if (S2.charAt(i) == 't') tCount++;

for (int i=0; i<S3.length(); i++) if (S3.charAt(i) == 'n') nCount++;

System.out.println("# of 'e' characters in 1st string: " + eCount); System.out.println("# of 't' characters in 2nd string: " + tCount); System.out.println("# of 'n' characters in 3rd string: " + nCount);

3. Write a Java program called DiagonalPrinting which reads a line of text from

the user and prints each word of the input line diagonally as illustrated in the sample

output below. Assume that two consecutive words in the input line of text are

separated by a single white space character. Note that to print a word consisting of

3 characters for example diagonally, precede the first character with 0 white spaces,

the second character with 1 white space, and the third character with 2 white spaces.

Sample output

Enter a line of text: DEAR ALL

D E A R A L L

import java.util.Scanner;

public class DiagonalPrinting {

public static void main (String[] args) {

Scanner scan = new Scanner(System.in);

System.out.print("Provide a line of text: ");

String S = scan.nextLine();

Scanner scanS = new Scanner(S);

while (scanS.hasNext()) { String P = scanS.next(); String padding = ""; for(int i=0; i< P.length(); i++) { System.out.println(padding + P.charAt(i)); padding += " "; } } } }