MidTerm_1D04_Post

# MidTerm_1D04_Post - Question 1 2 marks The following...

This preview shows pages 1–5. Sign up to view the full content.

Page 1 of 16 Question 1. 2 marks The following statements are found in a C# program. What is the final value of z ? int z, q; z = 6; q = 20; z = z % q; q = q – z; z = z + q / 2; Answer 1. A. 13 B. 15 C. 20 D. 27 E. 28 Question 2. 2 marks What is the value of z at the end of this code segment? int i = 4; int z = 5; for (i = 3; i > 0; i--) { z += 5 + i * i; } Answer 2. A. 33 B. 17 C. 21 D. 31 E. 34

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 2 of 16 Question 3. 2 marks What is the purpose of implementing a method? Answer 3. A. To speed up the code. B. To allow mathematical operations. C. To write a specific part of code once and use it many times D. To find the simplest solution. E. None of the above. Question 4. 2 marks How do you correctly subtract 1 from the variable a of type integer? Answer 4. A. a = -1; B. a = a * - 1; C. a += 1; D. a--; E. None of the above Question 5. 2 marks What is purpose of having the type-casting in the example below ( nrElements and nSum are declared as integers, dAverage is declared as double)? dAverage = (double)nSum / nElements; Answer 5. A. There is no need for type-casting, the division result is always a double. B. To inform the compiler that the result should be a double we need to type-cast at least one element of the division. C. The type-casting allows avoiding execution errors in case nElements is 0. D. The type-casting should be performed as (double)(nSum / nElements); E. None of the above.
Page 3 of 16 Question 6. 3 marks We have a text box called txtValue on our form. What is displayed in txtValue at the end of this code segment? int xyz = 0; for (int i = -1; i <= 1; i++) { xyz += i; } txtValue.Text = "Value is " + xyz; Answer 6. A. Value is 3 B. Value is +xyz C. Value is 0 D. Value is -1 E. Value is xyz Question 7. 3 marks int i=0; int j=5; int k=1; for(i=1;i<5 && j > 3;i++) { j--; k++; } <----- At this point in code: 1. Value of i = 3 2. Value of j = 1 3. Value of k = 3 Answer 7. A. Statements 1,2 are correct but 3 is wrong B. Statements 2,3 are correct but 1 is wrong C. Statements 1,3 are correct but 2 is wrong D. Statement 1 is correct but 2,3 are wrong E. Statement 3 is correct but 1,2 are wrong

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Page 4 of 16 Question 8. 3 marks string MethodExam (int a, int b) { int x = 0; if (a > x) return Convert.ToString(a) else return Convert.ToString(b) if (b > x) return Convert.ToString(b); else return Convert.ToString(a) } What will the above method return with parameters (0, -3)? Answer 8. A. 0 B. -3 C. "0" D. "-3" E. E. There is a compiler error: it should be only one return. Question 9. 3 marks What is the value of b after the last call of myMethod is executed? void myMain() { int b = 11; b = myMethod(b); b = myMethod(b+1); } int myMethod(int a) { if (a % 2 == 0) return 0; else return 1; } Answer 9. A.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/05/2010 for the course ENGINEERIN 1d04 taught by Professor Smith during the Spring '10 term at McMaster University.

### Page1 / 16

MidTerm_1D04_Post - Question 1 2 marks The following...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online