Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

ESC 101: Fundamentals of Computing Name Roll No Dept. Section

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ESC 101: Fundamentals of Computing Mid-sem Exam (16 Sep 2019)

Name 75 marks
Roll No Dept. Section Page 1 of 8

Instructions:
1. This question paper contains 4 pages (8 sides of paper). Please verify.
2. Write your name, roll number, department and section on every sheet of this booklet.
3. Write your final answers neatly with a blue/black pen. Pencil marks may get smudged.
Q1. Write T or F for True/False (write only in the box on the right hand side) (18x1=18marks)

1 __var__ is an invalid variable name in C

2 A variable cannot be used before it is declared

3 The following is a valid array initialization: int arr[5] = {'A', 1, -30};

4 An array declared as char a[500]; can only contain 499 characters.


The statements: int b = ~a + 1; and int b = -1 * a; are equivalent if
5
the variable a is an integer
The following code will always print 'true' (assuming a is an integer):
6 ((a>>1)<<1) == a ? printf("true") : printf("false");
The following code produces 'true' as output:
7 5&&2 ? printf("true") : printf("false");
8 The following code creates an infinite loop: int i = -100; while(i++);
Anything that can be done with a for loop can also be done with a while loop and
9
vice-versa
10 A break statement is necessary after every case in switch
The following code will correctly calculate the area of a triangle with base 2.5 and
11
height 3: float b = 2.5, h = 3; float area = b * h/2;
12 This will output 'E': char c = 'A'; printf("%c", c - 'a' + 'e');
All the elements of an array are stored contiguously (meaning sequentially, next
13
to each other) in memory
14 The print statement puts('a'); will give an error

15 The following code will always output 0: int a; printf("%d", a);


Not every real-valued number can be represented exactly using IEEE754 scheme
16
of representing floating point numbers
17 In a C program, using if without else will give syntax error
Assuming a is an integer, the loop while(a=0){printf(“Hello\n”);} will
18
run for infinite iterations.
Page 2 of 8
8
Q2. Fill the circle (don’t tick) next to the correct option (only one choice correct).(5x2=10marks)
2.1 Let P is a 16-bit signed integer. The 2’s complement representation of P is F87B (in hex).
Then, The 2’s complement representation of 4*P in hex is

A E1EC
B F87B
C F1EC #include<stdio.h>
int main(){
D E17B
int m=9900,n=13;
while(n!=0){
2.2 What will be the output of the program shown the right? int temp = m;
m = n;
A 7
n = temp%n;
B 100 }
C 761 printf("%d",m);
return 0;
D None of the above }
2.3 Which of the following statements is correct?
unsigned unsigned int a = 100; is a #include<stdio.h>
A int main(){
valid C statement
unsigned char x = 255;
Subtracting an integer from a character
B unsigned short a = x*256+260;
variable is not a valid operation
printf("%d",a);
Assuming short requires 2 bytes, the
C return 0;
output of C code on the right will be 4
}
D Every character array is also a string

2.4 Let a,b are integer variables. How many of the empty if statements in the table below will
give a compilation error?
A 6 if(b==0 || a= 1); if(b?0:a=1);
if(b?a=1:0); if(a=1 || b==0);
B 5 if(a=1==b=1); if(a==1=b==1);
C 4 if(return 0); if(a =+ 1);
D 3

2.5 The output produced by printf("happy \new year!"); will be

A happy \new year!


happy
B new year!
happy
C ew year!
D happy new year!
--------------------------- MAY USE SPACE BELOW FOR ROUGH WORK - - - - -- - - - - - - - - - - - - -
--------------
ESC 101: Fundamentals of Computing Mid-sem Exam (16 Sep 2019)
Name 75 marks
Roll No Dept. Section Page 3 of 8

Q3. Fill in the circles next to ALL CORRECT options (many may be correct). (5x3=15marks)
3.1 Which of the following are valid relational/logical expressions that can be used without
compilation error or run-time error, as the testing condition of an if-else statement? Assume that
a = b = c = 1 are integer variables.
A (a > b) || (a/0)
B ++a = b
C a >= c || a <= b
D 4%-3

3.2 Which of the following statements are true


#include <stdio.h>
after the execution of the program given on the
int main()
right hand side
{
A Value of i is -3 int i=-4,j=3,k=0,m;
B Value of j is 3 m = ++i || ++j && ++k;
C Value of k is 1 return 0;
}
D Value of m is 1

3.3 Which of the following statements are correct?


A The C statement if(int a=1); produces compilation error.
B ((3/0) && 2/5) will produce Run-Time Error on execution.
Using char main() instead of int main() then using return 0;
C in the end does not produce any compilation Error.
Consider two strings char str1[] = “Delhi”; and char str2[] =
D “Ranchi”; Using if(str1==str2); does not give run-time error.

3.4 Which of these are valid case labels for a switch statement, given that i=2 is an int variable?
A 3/5+5*9%2
B ‘0’
C (float)7
D i

3.5 Consider an array int arr1[] = {1,2,3,4}; Which of these will not give a compile/runtime error?

A int arr2[4] = arr1;


B int d = arr1[arr1[arr1[arr1[0]+arr1[0]]]];
C int i=1; arr1[i+++i]++;
D int i=5*arr1[1]=arr1[2];
Page 4 of 8
8 indicated.
Q4 In the space provided, write down the output of the program when given the input
Getting every output correctly carries equal weightage. (5+4+3+4=16marks)
4.1 Write down the output for the given input #include<stdio.h>
in the space provided int main(){
int n, count=0,max=2;
INPUT OUTPUT scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
20 arr[i] = 1;
}
for(int p=2;p*p<=n;p++)
50 if(arr[p]==1)
for(int j=p*p;j<n;j+=p)
arr[j] = 0;
for(int i=0;i<n;i++)
if(arr[i]){
count++;
max=i;
}
printf("%d %d",max,count);
return 0;
}

4.2 For the program shown on the right side, #include<stdio.h>


write down the output for the given input in int main(){
the space provided int n,key;
scanf("%d%d",&n,&key);
INPUT OUTPUT int arr[n];
for(int i=0;i<n;i++){
10 6 scanf("%d",&arr[i]);
}
23789 int start = 0;
11 15 22 int end = n-1;
56 98 while(start<end){
int middle = (start+end)/2;
25 4 printf("%d",middle);
if(arr[middle] == key){
12345 break;
6789 }
10 11 12 else if(arr[middle]<key){
13 14 15 start = middle+1;
}
16 17 18 else if(arr[middle]>key){
19 20 21 end = middle-1;
22 23 24 }
25 }
return 0;
}
ESC 101: Fundamentals of Computing Mid-sem Exam (16 Sep 2019)
Name 75 marks
Roll No Dept. Section Page 5 of 8

4.3 Write down the output for the


program shown on the right side #include <stdio.h>
int main (){
OUTPUT int a=6, b=1, c=8;
if(c > a ^ b)
printf (" First ");
if( b || a)
printf (" Second ");
else if(a < c)
printf (" Third \n");
printf ("%.2f ", (float)( a / c ));
printf ("%.2f ", ( a / (float)c ));
printf ("%d", c / a);
return 0;
}

4.4 Write down the output for the given input in the space provided

INPUT OUTPUT #include <stdio.h>


int main()
{
35 40 10 int n1,n2,b;
scanf("%d %d %d",&n1,&n2,&b);
int i,j,c=0;
12 8 2 for(i=n1,j=n2;i>0||j>0;i/=b,j/=b)
{
int s = (i%b) + (j%b) + c;
25 9 8 c = s/b;
printf("%d", s%b);
}
14 8 5 if(c)
printf("%d",c);
return 0;
}

--------------------------- MAY USE SPACE BELOW FOR ROUGH WORK - - - - -- - - - - - - - - - - - - -


--------------
Page 6 of 8
8 inside
Q5 In the following questions, you are either given incomplete code but with some hints
comments to complete the code or else given complete but buggy code. Fill in the blanks neatly
with code so that the program ends up doing what is specified in the question. If you need to
indicate a space, leave a small gap (don’t write a dot like Prutor). (4+4+8=16marks)
5.1 The following program is supposed to find the length of longest odd length palindrome which
is a substring of the given string. The maximum length of input string is 1024 characters. Read
the explanation in the example for better understanding of the algorithm. Fill in the blanks using
directions provided in the comments.
#include <stdio.h>
int main(){
char str[_______];
scanf(__________);
int max = 0, i;
// select the center of palindrome substring
for(_____________________){
int j;
// compare characters at offset j on the left and
// right of ith character
for(____________________________){
// exit the loop when character mismatch
if (___________________)
_____________________;
}
// update the maximum length
max = ______________ ? _________ : ________;
}
printf("%d\n", max);
return 0;
}

Example:
INPUT: cbacabd
OUTPUT: 5
Explanation: Iterate over the string “cbacabd”. Initially, centre of the palindrome substring is “c”,
and the max length of odd length palindrome is 1. In next iteration, “b” is the centre, but “c” !=
“a”, thus max length is again 1. We keep on iterating like this and we get to the fourth character
“c” for which left characters are equal to corresponding right characters up to first “c” != “d”, thus
the max length of odd length palindrome is 5. In further iterations, we will not get length greater
than this.
ESC 101: Fundamentals of Computing Mid-sem Exam (16 Sep 2019)
Name 75 marks
Roll No Dept. Section Page 7 of 8

5.2 The following program was written to read a sorted (ascending) array of integers and print
the number of duplicate(skipped) elements, and the unique elements in the array in decreasing
order along with their index.
Example:
Input Output
5 skipped:2
22344 3:4
2:3
1:2

However, the current code contains some errors which you need to correct. First write down
what output will this incorrect program give on the given inputs. You can also write output as
Compilation Error, Time Limit Exceeded, or, Other Error. Then correct the program by
pointing out line numbers and corrections to those line numbers. Frivolous and unnecessary
corrections may receive negative marks.

1 #include <stdio.h> INPUT OUTPUT


2 int main()
3 {
41123
4 // count -> no. of distinct int
5 int n, count, current, skip=0;
522344
6 scanf("%d",&n);
7 // arr -> No duplicate elements
8 // Store only distinct int
9 int arr[n]; Line Corrected
10 scanf("%d", &arr[count++]); No Code
11 current = arr[0];
12 for(int i=0;i<n;i++){
13 int read;
14 scanf("%d",&read);
15 if(read != current){
16 current = read;
17 arr[count] = current;
18 }
19 if(read == current)
20 skip++;
21 }
22 printf("skipped:%d\n",skip);
23 for(int i=n-1; i>=0; i--)
24 printf("%d:%d\n",++i,arr[i]); DO NOT SUGGEST CORRECTIONS
25 return 0;
26 } TO MORE THAN 7 LINES
Page 8 of 8
8
5.3 The program below reads a character, and based on that performs certain computations.
The explanation of each of the operation is given below.
• ' ' or '\n' – ignore
• 'r' – reads an integer, and a lower case character example: “5:c” (Input format) and
rotates the character by ‘a’ places. Example “5:c” outputs “h”.
• 'n' – reads an integer n and outputs space separated numbers from 1 to n.
• 'a' – reads an integer from 0 to 3 as array’s index and outputs the corresponding value.
Point out and correct all types of errors (compilation, runtime, logical). Frivolous and
unnecessary corrections may receive negative marks. Example of input and output:
Input Output
r 4:x n5 b
a2 12345
3
1 #include <stdio.h> Line No Corrected Code
2 int main(){
3 int ch = getchar();
4 while(ch != EOF){
5 if((ch == ' ')||(ch == '\n'))
6 break;
7 if(ch == 'r')
8 int a;
9 char b;
10 scanf("%d%c", &a,&b);
11 printf("%c\n", (b+a));
12 else if(ch == 'n'){
13 int n;
14 scanf("%d", &n);
15 for(i=1,i<n,i++)
16 printf("%d ", i);
17 printf("\n");
18 }
19 else if(ch == 'a'){
20 int a;
21 int arr[4] = {1,2,3,4,5};
22 scanf("%f", &a);
23 printf("%d\n", arr[a]);
24 }
25 ch = getchar();
26 }
27 return 0;
DO NOT SUGGEST CORRECTIONS
28 }
TO MORE THAN 12 LINES
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - END OF EXAM - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - -
--

You might also like