







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 13
This page cannot be seen from the preview
Don't miss anything!
The following precedence table is provided for your use:
Otherwise left to right
Total Possible Points =
Name
Precedence of Operators ( )
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.
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 + ”,”);
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
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” );
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.
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”);
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]+", "); } } }
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 }
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.