Sample Midterm Exam.pdf-2
Sample Midterm Exam.pdf-2
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.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
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
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;
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.
int main(void) {
//Variable Declaration
float num1, ;
3 sign;
if ( )
sign = ‘>’;
else
;
10 0;
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)