UM-SJTU Joint Institute Sample Midterm 2008 for Vg101 Introduction to Computer and Programming You Name (Printed): Student ID: General instructions You exam will be divided into two parts.A part on paper and the second one is on computer. For the on paper exam,you directly write down your answers on the examination paper,including any work that you wish to be considered for partial credit. Each question is marked with the number of points assigned to that problem.The total number of points is 50.We intend for the number of Total points to be roughly comparable to the number of minutes you should spend on that problem. Unless otherwise indicated as part of the instructions for a specific problem,comments will not be required on the exam.Uncommented code that gets the job done will be sufficient for full credit on the problem.On the other hand,comments may help you to get partial credit if they help us determine what you were trying to do. The examination is open-book,and you may make use of any texts,handouts,or course notes.You may not,however,use a computer of any kind for the paper test. For the computer part,you will be given a problem and you will need to design it,implement it, compile it,debug it and test it on computer.You need to summit your results to the ftp server specified in the problem description. THE UM-SJTU JI HONOR CODE I accept the letter and spirit of the honor code: "I have neither given nor received unauthorized aid on this examination,nor have I concealed any violations of the Honor Code." signature Date
UM-SJTU Joint Institute Sample Midterm 2008 for Vg101 Introduction to Computer and Programming You Name (Printed) : Student ID: General instructions You exam will be divided into two parts. A part on paper and the second one is on computer. For the on paper exam, you directly write down your answers on the examination paper, including any work that you wish to be considered for partial credit. Each question is marked with the number of points assigned to that problem. The total number of points is 50. We intend for the number of points to be roughly comparable to the number of minutes you should spend on that problem. Unless otherwise indicated as part of the instructions for a specific problem, comments will not be required on the exam. Uncommented code that gets the job done will be sufficient for full credit on the problem. On the other hand, comments may help you to get partial credit if they help us determine what you were trying to do. The examination is open-book, and you may make use of any texts, handouts, or course notes. You may not, however, use a computer of any kind for the paper test. For the computer part, you will be given a problem and you will need to design it, implement it, compile it, debug it and test it on computer. You need to summit your results to the ftp server specified in the problem description. THE UM-SJTU JI HONOR CODE I accept the letter and spirit of the honor code: "I have neither given nor received unauthorized aid on this examination, nor have I concealed any violations of the Honor Code." signature Date
Problem 1(20 pts).Expressions,Loops and Program Trace 1-a(10 pts):Find the value of x after each of the following expressions is executed.For each expression,assume that x and y are integers and that x has the value 15 prior to the execution of the expression.Put U in the answer box if you think the value cannot be determined. a)x/=x/y; b)x=sqrt (x+1)+2; c)x=y/x*x+y%x; d)x=!(x=x%4&&x<y%x); e)x=y *y--+--y; 1-b (10 pts):Loops a)(5 pts)Convert the following code containing a do/while loop to a code containing a while loop in such a way that the execution of the code is identical. intj=0; do j=j+1; console.printLine (j,"\n"); while(j<=10); console.printLine j,"\n"); b)(5pts):Given bellow is an algorithm in pseudo-code: START For which of the following inputs will the OUTPUT“Enter a positive integer'” algorithm say“YES"? NPUT N i=1 (a28 YES▣ NO▣ WHILE(i*i<N) (b)36 YES▣ NO▣ i=i+1 (c)54 YES口 NO▣ END WHILE (d)81 YES▣ NO▣ IFi×i=N) OUTPUT“YES” What property of the input is the algorithm ELSE determining? OUTPUT“NO” END IF STOP
Problem 1 (20 pts).Expressions, Loops and Program Trace 1-a (10 pts): Find the value of x after each of the following expressions is executed. For each expression, assume that x and y are integers and that x has the value 15 prior to the execution of the expression. Put U in the answer box if you think the value cannot be determined. a) x /= x / y; b) x = sqrt (x+1) + 2; c) x = y / x * x + y % x; d) x = !(x = x % 4 && x < y % x); e) x = y * y-- + --y; 1-b (10 pts): Loops a) (5 pts) Convert the following code containing a do/while loop to a code containing a while loop in such a way that the execution of the code is identical. int j = 0; do { j = j + 1; console.printLine ( j, "\n"); } while (j <= 10); console.printLine ( j, "\n"); b) (5pts): Given bellow is an algorithm in pseudo-code: START OUTPUT “Enter a positive integer” NPUT N i = 1 WHILE (i * i < N) i = i + 1 END WHILE IF (i × i = N) OUTPUT “YES” ELSE OUTPUT “NO” END IF STOP For which of the following inputs will the algorithm say “YES”? (a) 28 YES NO (b) 36 YES NO (c) 54 YES NO (d) 81 YES NO What property of the input is the algorithm determining?
1-c(10 pts):Think like a computer //Consider the following C++code. #include <Vg101Class.h> using namespace std; #define NMax 1000 #define EPSILON 0.0001 int main(void) ConsoleT console; double x console.readDouble("Input x(must be positive):") double xPower 1.0,lastTerm,sum =0.0; int i=0; do i=i+1; xPower x*xPower; lastTerm xPower/i; sumsum lastTerm; while(i<NMax &lastTerm>EPSILON); if (i =NMax)console.printLine("Series did not convergeln"); else f console.printLine ("i=",i,"x=",x,"lastTerm "lastTerm,"sum ="sum,endl); i return 0; } (a)What series is being summed by this code?(4 pts) sum=x+ (b)If the user inputs-1.0 for x,what will be printed out by this code?(2 pts) (c)If the user inputs 1.0 for x,what will be printed out by this code?(2 pts) (d)If the user inputs 0.01 for x,what will be printed out by this code?(2 pts)
1-c (10 pts): Think like a computer //Consider the following C++ code. #include <Vg101Class.h> using namespace std; #define NMax 1000 #define EPSILON 0.0001 int main(void) { ConsoleT console; double x = console.readDouble ("Input x (must be positive): "); double xPower = 1.0, lastTerm, sum = 0.0; int i = 0; do { i = i + 1; xPower = x*xPower; lastTerm = xPower/i; sum = sum + lastTerm; } while (i < NMax && lastTerm > EPSILON); if (i == NMax) console.printLine ("Series did not converge\n"); else { console.printLine ("i = ", i, "; x = " , x, "; lastTerm = ", lastTerm, "; sum = " , sum, endl); }; return 0; } (a) What series is being summed by this code? (4 pts) sum = x + (b) If the user inputs -1.0 for x, what will be printed out by this code? (2 pts) (c) If the user inputs 1.0 for x, what will be printed out by this code? (2 pts) (d) If the user inputs 0.01 for x, what will be printed out by this code? (2 pts)
Problem 2(10 pts):Functions and Parameter Passing:write out the outputs of the following program #include <Vg101Class.h> using namespace std; int SpaceMountain(double donald,int walt); void Matterhorn (int&mickey,int disney); int main (void) ConsoleT console: int mickey; int goofy; double donald 2.6; Mickey Goofy Donald mickey goofy donald; console.printLine (mickey,""goofy,"",donald,endl); for (int i=0;i<2;i++) mickey SpaceMountain (donald,goofy mickey); } console.printLine (mickey,"",goofy,""donald,endl); goofy++; goofy /mickey: console.printLine(mickey,"".goofy,"",donald,endl); Matterhorn(mickey,goofy): console.printLine (mickey,"",goofy,""donald,endl); return 0: int SpaceMountain(double donald,int walt) donald +3; return (walt *2); } void Matterhorn(int&mickey,int disney) disney mickey +(2 disney); mickey disney 3;
Problem 2 (10 pts): Functions and Parameter Passing: write out the outputs of the following program #include <Vg101Class.h> using namespace std; int SpaceMountain(double donald, int walt); void Matterhorn (int& mickey, int disney); int main (void) { ConsoleT console; int mickey; int goofy; double donald = 2.6; mickey = goofy = donald; console.printLine (mickey, " ", goofy, " ", donald, endl); for (int i = 0; i < 2; i++) { mickey = SpaceMountain (donald, goofy + mickey); } console.printLine (mickey, " ", goofy, " ", donald, endl); goofy++; goofy /= mickey; console.printLine (mickey, " ", goofy, " ", donald, endl); Matterhorn (mickey, goofy); console.printLine (mickey, " ", goofy, " ", donald, endl); return 0; } int SpaceMountain(double donald, int walt) { donald += 3; return (walt * 2); } void Matterhorn (int& mickey, int disney) { disney = mickey + (2 * disney); mickey = disney % 3; } Mickey Goofy Donald
3:Debug Programming (10 pts) Write a C++Program to calculate the following sum: S=1-X2+X4-X5+X8-X10 The code has both syntax errors as well as "design"flaws in some lines.Design flaws are those that compile without error but do not provide correct answers upon execution.Fix these as well.The corrected code should be able to compile and calculate the above sum. #include <Vg101Class.h> using namespace std; main(void) int x console.readDouble ("Input x:) int xPower =1; int sum 0; int sign; for (int term =1,;term 10;term ++ xpower *sign *x xPower; sumsum xPower; ; console.printLine ("S ="sum,endline);
3: Debug & Programming (10 pts) Write a C++ Program to calculate the following sum: S = 1 – X2 + X4 – X6 + X8 – X10 The code has both syntax errors as well as “design” flaws in some lines. Design flaws are those that compile without error but do not provide correct answers upon execution. Fix these as well. The corrected code should be able to compile and calculate the above sum. #include <Vg101Class.h> using namespace std; main(void) { int x = console.readDouble ("Input x: ); int xPower = 1; int sum = 0; int sign; for (int term = 1,; term < 10; term ++) { xpower *= sign * x * xPower; sum = sum + xPower; }; console.printLine ("S = ", sum, endline); }