0% found this document useful (0 votes)
4 views

Sample Midterm Exam.pdf-2

This document is a sample exam for the course 'Introduction to Algorithms and Programming I' from the School of Computer Science, dated Winter 2018. It includes instructions for the exam, multiple-choice questions, and coding tasks related to C programming. The exam consists of various questions testing knowledge on programming concepts, syntax, and flowchart interpretation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Sample Midterm Exam.pdf-2

This document is a sample exam for the course 'Introduction to Algorithms and Programming I' from the School of Computer Science, dated Winter 2018. It includes instructions for the exam, multiple-choice questions, and coding tasks related to C programming. The exam consists of various questions testing knowledge on programming concepts, syntax, and flowchart interpretation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

School of Computer Science

60-140-30 Introduction to Algorithms and Programming I


Winter 2018
Sample Exam 1

Thursday, Feb. 8, 2018


Student Name: ________________ ___________________
First Name Family Name

Student ID Number: _____________________


Duration of examination: 70 minutes
1. Answer all questions on this examination paper in the space provided.
2. This is a closed-book examination – no notes or books or electronic
computing or storage devices may be used.
3. Do not copy from other students or communicate in any way. All
questions will be answered only by the attending proctors.
4. All students must remain seated during the last 5 minutes of the
examination.
5. The examination must be surrendered immediately when the instructor
announces the end of the test period.
6. Each student must sign the examination list before leaving the
classroom.
Total mark obtained: ____________________
Maximum mark: 40
NOTE: You may use the reverse side of any page to continue an answer to a
question, or for rough work. Clearly identify each such continuation by a
question number (and part letter).

NOTE: This is a sample of questions that have been asked previously and must not be taken as an
indicator of what questions will be asked in any particular examination this semester. The purpose of
providing this file is to show students the nature of questions that might be asked, the number and marking
weight of some typical questions and to provide a different kind of perspective on how to prepare. It is
extremely important that students read questions thoroughly (multiple times) and consider them carefully
before proceeding to write a solution. Many errors are committed due to lack of understanding of what is
being asked for. Remember, you get marks for answering the question that is asked, not for your own
made-up version of another question.

Page 1/7
This study source was downloaded by 100000867211055 from CourseHero.com on 02-16-2025 02:09:09 GMT -06:00

https://www.coursehero.com/file/43997665/Sample-Midterm-Exampdf/
Question 1. [ 20 marks ]
Answer multiple choices questions (each 1 mark):

1.1 How many tokens does the following statement consist of: ANS: (c)
printf(“Hello, %s\n”, name);
a) 5 b) 6
c) 7 d) 8

1.2 Which of the following is NOT a valid variable name? ANS: (a)
a) 1stQuiz b) _num
c) iNt d) my_first_name

1.3 Which of the following is correct when printing a backslash ( \ )? ANS: (b)
a) printf(“\”); b) printf(“\\”);
c) Both a) and b) are correct d) Neither are correct

1.4 To define two variables that store integer numbers, which of the following statement is
correct? ANS: (b)
a) integer number1, number2;
b) int number1, number2;
c) float number1, number2;
d) int number1, int number2;

1.5 How many bits are in a byte? ANS: (b)


a) 4 b) 8
c) 10 d) 12

1.6 Size of ….. is one byte. ANS: (c)


a) int b) float
c) char d) double

1.7 What is a valid form for testing whether two variables are equal? ANS: (b)
a) if (i = j) b) if (i == j)
c) if (i & j ) d) if (i || j)

1.8 Which of the following answers is a valid form of a comment? ANS: (c)
a) /* This is a comment b) / This is a comment
c) // This is a comment d) Both a) and c)

Page 2/7
This study source was downloaded by 100000867211055 from CourseHero.com on 02-16-2025 02:09:09 GMT -06:00

https://www.coursehero.com/file/43997665/Sample-Midterm-Exampdf/
1.9 What is the output of printf("%d",11/2)? ANS: (a)
a) 5 b) 6
c) 5.5 d) 5.5000000

1.10 To print a float number, which of the following conversion specification is needed?
a) %i b) %f ANS: (b)
c) %c d) %d

1.11 What is the appropriate way to assign the letter X to a character variable called
letter? ANS: (c)
a) letter = X; b) letter = “X”;
c) letter = ‘X’; d) All options

1.12 Which of the following statement is correct? ANS: (d)


a) i + j = 0;
b) printf("%d %d\n", &i, &j);
c) scanf("%d\n", i);
d) scanf("%d%d%f%f", &i, &j, &x, &y);

1.13 Suppose A = 5, B = 3, and C = 4 respectively. Choose a set of operations


where the result is 17? ANS: (b)
a) A * (B + C)
b) A + B * C
c) A * B + C
d) (A + B) * C

1.14 What is the output of the following code? ANS: (b)


a) 5 int a, b=2, c=3;
b) 6 a= ++b + c++;
c) 7 printf(“%d“, a);
d) 8

1.15 What is the output of the following code? ANS: (b)


a) -1 int x, y = 3;
b) 0
x = (++y < 4 ? 1 : -1) + 1;
c) 1
d) 2 printf(“%d“, x);

Page 3/7
This study source was downloaded by 100000867211055 from CourseHero.com on 02-16-2025 02:09:09 GMT -06:00

https://www.coursehero.com/file/43997665/Sample-Midterm-Exampdf/
1.16 What will be the output after execution of the following program segment? ANS: (c)
a) 10.12
b) 10.120 float var1 = 10.12;
c) 10.12000 printf(“%.5f”, var1);
d) 10.120000

1.17 What will be the output after execution of the following program segment? ANS: (d)
a) c97a
b) aac char c = 'a';
c) a97a printf("%c%i%c", c, c, 'c' );
d) a97c

Use the given flowchart to answer questions 1-18:20

1.18 What will figure 2 be used for? ANS: (a)


a) Reading Input
b) Processing
c) Printing Output
d) Starting/Ending the program

1.19 What will figure 5 be used for? ANS: (c)


a) Reading Input
b) Processing
c) Printing Output
d) Starting/Ending the program

1.20 How many processing units are there in the given flowchart?
ANS: (c)
a) 0
b) 1
c) 2
d) 3

Page 4/7
This study source was downloaded by 100000867211055 from CourseHero.com on 02-16-2025 02:09:09 GMT -06:00

https://www.coursehero.com/file/43997665/Sample-Midterm-Exampdf/
Question 2. [ 5 marks ]

Rewrite the following piece of code by replacing the else-if statements with a switch
statement. The logic of the program should be the same.
if (num == 1)
printf(“One \n”); switch (num){
case 1: printf("One \n");
else if (num == 2) break;
printf(“Two \n”);
case 2: printf("Two \n");
else if (num == 3) break;
printf(“Three \n”);
case 3: printf("Three \n");
else break;
printf (“*** \n”);
defualt: printf ("*** \n");
}

Question 3. [7 marks ]
Consider the following C language programs. What is the output of each program?

Part A. [ 3 marks ]

#include <stdio.h>
int main (void)
{
int a = 25, b = 3, c;
float floatVar;

floatVar = c = 42.37f;

printf ("Result 1 = %d\n", 10 % 4 * 2 + b - ++a);


printf ("Result 2 = %d\n", a / b * b);
printf ("Result 3 = %.2f\n", floatVar);

return 0;
}

ANSWER:
ANSWER:
Result 1 = -19
Result 2 = 24
Result 3 = 42.00

Page 5/7
This study source was downloaded by 100000867211055 from CourseHero.com on 02-16-2025 02:09:09 GMT -06:00

https://www.coursehero.com/file/43997665/Sample-Midterm-Exampdf/
Part B. [ 2 marks ]

#include <stdio.h>
int main(void)
{
int i = 0, j = 5;
if (j > 5 && ++i >0) printf("A");
else printf("B");
if (i++ > 0 || j < 0) printf("C");
else printf("D");
printf("%d", i);
return 0;
}

ANSWER:
ANSWER:
BD1
9 8

Part C. [ 2 marks ]

#include <stdio.h>
int main (void) {
int n = 2;

switch(n){
case 2: n += 1;
case 0: n += 2; break;
case 1: n += 3; break;
default: n += 4;
}
printf("n = %d", n);
return 0;
}

ANSWER:
n = 5

Page 6/7
This study source was downloaded by 100000867211055 from CourseHero.com on 02-16-2025 02:09:09 GMT -06:00

https://www.coursehero.com/file/43997665/Sample-Midterm-Exampdf/
Question 4. [ 10 marks ]

Considering the following flowchart, complete the equivalent C program that accomplishes
what the flowchart does.

#include < >

int main(void) {

//Variable Declaration
float num1, ;

3 sign;

// Reading input value


printf ("First number: \n");
scanf (" ", &num1);

printf ("Second number: \n");


scanf ("%f", );

if ( )
sign = ‘>’;
else
;

9 printf(" %c %f \n”, num1,sign,___);

10 0;

1- stdio.h 2- num2 3- char 4- %f

5- &num2 6- num1 >= num2 7- sign = ‘<’ 8- %f

9- num2 10- return

Good Luck
Dr. Mina Maleki

Page 7/7
This study source was downloaded by 100000867211055 from CourseHero.com on 02-16-2025 02:09:09 GMT -06:00

https://www.coursehero.com/file/43997665/Sample-Midterm-Exampdf/
Powered by TCPDF (www.tcpdf.org)

You might also like