0% found this document useful (0 votes)
13 views4 pages

Midsem

Uploaded by

aishwaryim
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)
13 views4 pages

Midsem

Uploaded by

aishwaryim
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/ 4

CSF111–Exam I (2/5/23) 100 marks

Name: BITSID:

The exam has 8 questions on two separate sheets (4 pages). Write your ID on all pages
and submit both parts.
Answers written anywhere except in the designated space on the question paper will not
be graded. Use the given answer-books as rough sheets and turn them in.
Assume all the relevant variables are declared and all relevant header files are included.
Where not specified, assume the code is within main .
If a question has a typo, don’t bother the invigilators. Answer it with ‘This question has
a typo’ and get full marks.

Part I [1M ] (b) If your function passes all test cases, your
1. What does this C code print on the console? function is bug-free.

[3M ] (a) int n1 = 15 , n2 = 4; False


double n3 = n1 / n2 ;
[1M ] (c) In C, you can have an empty else-block,
n1 /= n3 ;
printf ( " %d , %d , % lf " , but not an empty if-block.
n1 , n2 , n3 ) ;
False
5, 4, 3.00000 [1M ] (d) Syntax errors are typically caught early.
[4M ] (b) int x = 10;
printf ( " %d , " , x ++) ; True
{ [1M ] (e) Compilers translate a high-level language
printf ( " %d , " , x ) ; code to machine language.
int x = 9;
printf ( " %d , " , ++ x ) ;
}
True
printf ( " % d " , x ) ;
3. Short answers.
10, 11, 10, 11 [4M ] (a) What does the call convert (23) print
on the console?
[4M ] (c) int power ( int m , int n ) { /* *
int ans = 1; * @brief ...
if ( n > 0) { * Requires n >= 0
n - -; */
ans = m * power (m , n ) ; void convert ( int n ) {
} if ( n > 0) {
return ans ; int rem = n % 2;
} convert ( n /2) ;
printf ( " % d " , rem ) ;
int main () { }
int m = 5; }
int n = 3;
printf ( " % d ^ % d = % d " ,
m , n , power (m , n ) ) ; 10111
return 0;
} [8M ] (b) In the following C code, identify at least
four expressions by underlining them and
5 ^ 3 = 125 at least four statements by drawing a
box around them. Expressions:
2. Answer whether each statement is True or 0,
False. int sum = 0;
1,
[1M ] (a) If you need to represent integer values int current = 1; sum and current
greater than INT_MAX , you should use if ( current <= num ) { (not in declaration),
the double data type. sum + current,
sum = sum + current ; etc.
False } Statements:
int sum = 0;
int current = 1;
Page 1 of 4 entire if statement from if to the end of }
sum = sum + current;
CSF111–Exam I (2/5/23) BITSID:

[4M ] (c) const int THRESHOLD = 10; [6M ] (b) Write the expressions to replace e3 , e4
int x = THRESHOLD - 1; and e5 so the function computes the tax
int y = x ; based on the income ( in ) as follows:
income is 5 lakhs(L) or below: no
if ( x < THRESHOLD ) { y - -; }
MYSTERY
tax,
if ( y < THRESHOLD ) { x - -; } income is above 5L but up to 10L
(inclusive): no tax on the first 5L,
printf ( " %d , % d " , x , y ) ; 10% tax on the rest,
income is above 10L: no tax on the
What is the output of this C code if
first 5L, but 20% on the rest.
(i) MYSTERY is replaced with a blank (no
code)? double tax ( double in ) {
double ans = 0;
8, 8 if ( e3 )
ans = e4 * ( in - 500000) ;
(ii) MYSTERY is replaced with else ? if (1000000 < in )
ans = e5 * ( in - 500000) ;
9, 8
return ans ;
[6M ] (d) What does print2 (4) print on the con-
}
sole? Assume n >0 for both functions and
c a printable character. in > 500000 && in <= 1000000
void print1 ( char c , int n ) { or
if ( n > 1) in > 500000
e3
print1 ( c - 1 , n - 1) ;
printf ( " % c " , c ) ; 0.1 or 10.0/100
} e4 or something equiv. but must be a double
void print2 ( int n ) {
0.2 or 20.0/100
if ( n > 1) e5
print2 ( n - 1) ; or something equiv. but must be a double
print1 ( ‘0 ’ + n , n ) ; [3M ] (c) Write the expression to replace e6 so
printf ("\ n ") ; that the function xor returns true i↵
} exactly one of its parameters is true .
bool xor ( bool m , bool n ) {
1 P return e6 ;
12 PQ } Several possible answers.
123 PQR Here is one: (m && !n) || (!m && n)
1234 PQRS Check your solution against the four
e6 valid inputs 00,01,10,11
Two possible answers based on [4M ] (d) Write the statement to replace s1 and
whether ‘0’ is considered zero or expression to replace e7 so the following
the letter O; both correct. code takes two integers from the user and
prints their sum. However, if the sum is
4. Code completion. a negative number or a multiple of 7, it
[6M ] (a) Write the expressions to replace e1 and prints “wrong input” instead of the sum.
e2 so that the given function returns
int x , y ;
m ⇥ n, without using the * operator. As- printf ( " Enter two integers : " ) ;
sume both m and n are positive. s1
int product ( int m , int n ) { int sum = x + y ;
int ans = m ; if ( e7 )
if ( e1 ) printf ( " wrong input " ) ;
ans = e2 ; else
return ans ; printf ( " % d \ n " , sum ) ;
}
scanf(“%d%d”, &x, &y);
n>1 s1
e1
sum < 0 || sum % 7 == 0
e2 ans + product(m, n-1) e7
or
m + product(m, n-1)
Page 2 of 4
CSF111–Exam I (2/5/23) BITSID:

Part II
[10M ] 5. Write a function that returns the next state of the traffic light. The lights circle in this order: R -> G
-> Y -> R . Requires: current is either ‘R ’ or ‘G ’ or ‘Y ’ .
Rearrange these lines to produce a correct function. Not all lines are part of the solution. You can use
a given line only once. Only write the sequence of numbers as your answer.
1. {
2. }
3. return next ;
4. char traffic_light ( char current )
5. return traffic_light ( current - 1) ;
6. char next ;
7. next = ‘R ’;
8. next = ‘Y ’;
9. next = ‘G ’;
5 or 13 cannot appear in a correct solution
10. else
11. if ( current == ‘G ’) Most common problem was ‘dangling else’, i.e., the else getting attached
12. if ( current == ‘R ’) only to the _last_ if. Trace your solution against the three inputs ‘R’, ‘G’, ‘Y’.
13. return current ;

Several variants possible: - 4, 1, 6, 7, 12, 9, 10, 11, 8, 3, 2


- 4, 1, 6, 7, 11, 8, 10, 12, 9, 3, 2 - 4, 1, 6, 7, 12, 9, 11, 8, 3, 2 - 4, 1, 6, 11, 8, 10, 7, 12, 9, 3, 2
- 4, 1, 6, 7, 11, 8, 12, 9, 3, 2 - 4, 1, 6, 12, 9, 10, 7, 11, 8, 3, 2

[12M ] 6. Identify the type and value of each of the following expressions based on these variable initialisations:
int m = 10;
Types in C are case sensitive: int ≠ Int or Integer
int n = 15;
double x = 2.5; Expr. no. 2 and 3: type must be either int or bool (not int/bool),
and value must be written accordingly
Expression Type Value
m + n * 2
int 40
n / m <= 1 int 1
or bool or true
m + n / 5 == 5 int 0
or bool or false
m + n / (5 == 5)
int 25
x * (m + n)
double 62.5
x * m + n
double 40 or 40.0

[5M ] 7. Write the full header for a function that takes three real numbers and reports if the relative error between
the first two is less than the third number. Relative error between x and y is |x y|/x. Remember, a
function’s header consists of the return type, the function name, and the parameter list. Do not provide
the function body.

bool is_within_relative_error(double x, double y, double epsilon)

Return type can be int


Function name and param names can be differrent.
Should not show any function body or any other code.

Page 3 of 4
CSF111–Exam I (2/5/23) BITSID:

[4M ] 8. (a) Implement the following C function to match the purpose/contract. Note: you don’t need recursion
for this part.
/* *
* @brief Returns whether the given non - negative number is even .
*
* Requires n >= 0
* @retval true is_even (42)
* @retval false is_even (43)
*/
bool is_even ( int n )

Several variants possible,


here is the simplest one Trace for several test cases:
0
{ 1
return n % 2 == 0; 42
} 43

It should not have any syntax errors or additional printing statements

[12M ] (b) Implement the following C function to match the purpose/contract. You must use recursion. Do
not create any other helper functions. You must call is_even declared above.
/* *
* @brief Reports whether the sum of digits of the given
* non - negative number is even .
*
* Requires n >= 0
* @retval true is _s um _d ig its_ even (53)
* @retval false is _s um _d ig its_ eve n (179)
*/
bool i s_ su m_di gi ts _e ven ( int n )

One possible solution→


{
bool ans = is_even(n);
if (n > 9) {
int units = n % 10;
ans = (is_even(units) && is_sum_digits_even(n/10))
|| (!is_even(units) && !is_sum_digits_even(n/10));
}
return ans;
}

==
Trace for several test cases:
0
5
53
54
536
537

It should not have any syntax errors or additional printing statements
It must be solved using recursion
It should not rely on helper function to count digits, sum of digits, etc.

Page 4 of 4

You might also like