Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Sample Final Exam - Introduction to Computing I | CSE 020, Exams of Computer Science

Material Type: Exam; Professor: Lwin; Class: Introduction to Computing I; Subject: Computer Science & Engineering; University: University of California-Merced; Term: Fall Semester 2015;

Typology: Exams

2014/2015

Uploaded on 11/22/2015

mhernandez401405
mhernandez401405 🇺🇸

2 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
!1
Sample Sample Sample Sample Sample
!
CSE 20!
!
SAMPLE FINAL
Time: 180 minutes
!
!
!
!
!!
!
!
The following precedence table is provided for your use:!
!
Otherwise left to right
!
Total Possible Points =
!
Name
!
ANSWERS
Precedence of Operators
( )
- (unary), !, ++, --
*, /, %
+ , - (binary)
<, <=, >, >=
= =, !=
&&
||
=, +=, -=, *=, /=, %=
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Sample Final Exam - Introduction to Computing I | CSE 020 and more Exams Computer Science in PDF only on Docsity!

CSE 20

SAMPLE FINAL

Time: 180 minutes

The following precedence table is provided for your use:

Otherwise left to right

Total Possible Points =

Name

ANSWERS

Precedence of Operators ( )

  • (unary), !, ++, -- *, /, %
  • , - (binary) <, <=, >, >= = =, != && || =, +=, -=, *=, /=, %=
  1. Match the following statements (i-x) to the outputs (A-J) they produce. Note: that there are no duplicate answers (‘G’ has the value 71)

int i=3, j=2; double d=5.0; char c = 'G'; // G is 71 i. System. out .println("OUTPUT is " + (( int )c + j) ); ______g________ ii.System. out .println("OUTPUT is " + (( char )c - j) ); ______e________ iii.System. out .println("OUTPUT is " + ( char )(c - j )); ______b________ iv.System. out .println("OUTPUT is " + ( char )c + j ); ______c________ v. System. out .println("OUTPUT is " + ( char )(c - d) ); ______a________ vi.System. out .println("OUTPUT is " + (( char )c - d) ); ______d________ vii.System. out .println("OUTPUT is " + ( double )c + j ); ______f________ viii.System. out .println("OUTPUT is " + ( double )c / j * d); ______j________ ix.System. out .println("OUTPUT is " + c / ( char )i * d); ______h________ x.System. out .println("OUTPUT is " + c / j * ( int )d); ______i________

a) OUTPUT is B b) OUTPUT is E c) OUTPUT is G d) OUTPUT is 66. e) OUTPUT is 69 f) OUTPUT is 71. g) OUTPUT is 73 h) OUTPUT is 115. i) OUTPUT is 175 j) OUTPUT is 177.

  1. The following code is supposed to write out the numbers from 5 to 1. It has two flaws.

a) What is the output of the code as it is written?

b) Correct the code so that it works as intended. Write the corrected version of the main function in the box below. There is more than one way to fix the problems.

public class Loop {

public static void main(String[] args) { int i;

for(i=10; i>=0; i--) if (i / 2 == 0) System.out.print(i/2 + ”,”); } // end of main

} // end of Loop

for(i = 10; i > 1; i--) if (i % 2 == 0) System.out.print(i/2 + ”,”);

  1. For each value of n below, give the output from the execution of the following program segment a. Consider While loop

i. n = 4

ii. n = -

b. Consider Do-While loop

i. n = 4

ii. n = -

c. Consider the If statements

i. n = 3

ii. n = 99

END.

int i = 1, n; n = keyboard.nextInt();

while ( i < n){ System.out.print(i+" "); i *= i; } System.out.println("END."); int i = 1, n; n = keyboard.nextInt(); do { System.out.print(i+“ “); i *= i; } while ( i < n);

System.out.println(“END.”); 1 1 1 1 1 1 1 1 … 1 END. red purple green blue int i = 3, n; n = keyboard.nextInt(); if (i == n) System.out.println( “red” ); else System.out.println( “green” ); if (i < n) System.out.println( “blue” ); else System.out.println( “purple” );

  1. a) Consider the code below on the left hand side. The right hand size represents the array created. Fill-in the values of each entry after the code is executed. The first two lines are filled in as an example.

b) Consider the code below on the left hand side. The right hand size is the output. If the code does not output anything then leave it blank or write BLANK. First two answers are given as an example.

int [] n = new int[6]; 0 0 0 0 0 0 int i = 3; n[0] = 5; 5 0 0 0 0 0 n[i] += 5; 5 0 0 5 0 0 n[2+i] = n[0] + 1; 5 0 0 5 0 6 n[4-i] += 1; 5 1 0 5 0 6 n[1] = n[0] + n[1]+ n[2] + n[3] + n[4]; 5 11 0 5 0 6 n[1+2] = 1+i; 5 11 0 4 0 6 n[n.length-1] = n[0]; 5 11 0 4 0 5 n[n.length-i] = n.length-2; 5 11 0 4 0 5 n[i*2-1] = n[2] * n[2]; 5 11 0 4 0 0 int i = 0; BLANK System.out.println(i); 0 System.out.println(i+2); 2 i *= 2; BLANK System.out.println(“Num is” + i); Num is 0 System.out.println(i++); 0 i += 2.0; BLANK System.out.println(++i); 4 System.out.println(i % 2 ); 0 System.out.println(i += 4); 8 System.out.println(i = i * 4); 32 System.out.println (i / 3.0); 10.

  1. a) Suppose a program reads a positive integer from the keyboard into the integer variable array_length. Then it will allocate space for an array of array_length floats. Use any of the following variables declared already in the rest of the program. You should not use any additional variables.

b) Declare and allocate an array of 1000 integers. Write a loop which fills the array locations with their reverse indices. For example, slot 0 of the array should hold the value 999, and slot 998 of the array should hold the value 1.

c) Assume you have an array of doubles which has already been filled with data read in from the user. Assume this array is called scores. We are not sure how long the array is, but we know it is completely full of data. Write code which calculates the average of the POSITIVE values in the scores array. If there are no positive numbers then print out “Array contains no positive numbers”, avoid any run-time errors.

int array_length; Scanner input = new Scanner(System.in);

double[] d_arr; int[] i_arr; float[] f_arr;

// Start here System.out.print(“Enter the max ”); array_length = input.nextInt(); f_arr = new float[array_length]; int [] arr = new int[1000];

for (int i = 0; i < arr.length; i++) arr[i] = arr.length – i - 1; double sum = 0.0; int count = 0;

for (int i = 0; i < scores.length; i++) if (scores[i] > 0) { sum += scores[i]; count++; }

if (count > 0) System.out.println(“Average is ” + sum/count); else System.out.println(“Array contains no positive numbers”);

  1. Given the program shown below indicate the output in the box below. This is one continuous program. Follow each step carefully, you may want to draw the array created with values in each entry to help you answer this question. ( partial credit )

Output public class ProblemNine {

public static void main(String[] args) { int LITTLE = 5, MEDIUM = 10, BIG = 20;

int i,j, n = 9,temp; int[] num= new int[BIG];

num[0]=9; num[1]=3; num[2]=4; num[3]=8; num[4]=2; num[5]=1; num[6]=5; num[7]=6; num[8]=7; num[9]=10;

num[LITTLE] = 1; i = MEDIUM; while (i < BIG) { num[i] = 2*num[i % MEDIUM]; i++; }

for (j=0;j<BIG;j++) { if (j % LITTLE == 0) System.out.println(); System.out.print(num[j]+", "); } } }

  1. Find and fix the bug(s) in the following code. For each line, indicate the line number, whether it is (C)ompile- time, (R)un-time, or (L)ogical, it can have multiple errors so circle all relevant ones. In the last entry, give the corrected code or SAME if there are no errors and changes needed. /*
  • Calculate the sum of all the elements Squared from the array */

0 public static void main (String [] args) { 1 double[] inp = {1.0, 2.0. 3,.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0} 2 int sum;

3 for (int i = 1; i <= inp.length-1; ++i ) { 4 Sum = inp[i] + inp[i*i]; 5 } 6 System.out.println(“Sum of Squares is ” + sum); 7 }

Line

Error Type Corrected Code

0 C^ L^ R^ SAME

1 C^ L^ R^ double[] inp = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};

2 C^ L^ R^ double sum = 0.0;

3 C^ L^ R^ SAME

4 C^ L^ R^ sum += inp[i] * inp[i];

5 C^ L^ R^ SAME

6 C^ L^ R^ SAME

7 C^ L^ R

  1. Answer if each of the following statements are valid or invalid. Invalid means Eclipse will flag it as an error.

int i = 0; // Valid Invalid int max = 10; int count; int [] arr = new int [max-1]; // Valid Invalid for ( int i = 0; i < max; i++); // Valid Invalid System. out .println(arr[i] = i); // Valid Invalid while (i < max) { i += 2; // Valid Invalid if (i % 2 == 0) { int j = 1; for (j = max-2; j >= 0; j--) // Valid Invalid System. out .println(arr[j]); count++; // Valid Invalid } else { int count = j; // Valid Invalid for ( int j = max-2; j >= 0; j--) // Valid Invalid System. out .println(arr[j]); count--; // Valid Invalid } }

count++ and count-- could be marked invalid due to no initialization when count was declared. So either answer would be okay for this problem as the statement themselves are fine.