Você está na página 1de 6

I SEMESTER B.TECH.

INTERNAL EXAMINATIONS FEBRUARY 2019


TEST – 1
SUBJECT: PROBLEM SOLVING USING COMPUTERS [CSE 1051]
Date of Exam: xx/02/2019 Time of Exam: 5.30 PM – 6.30 PM Max. Marks: 15
Instructions to Candidates:
 Answer ALL the questions in ORDER & missing data may be suitable assumed
 For MCQs, there is a single correct answer. It is mandatory to write both option and answer
 Indicate all the steps where ever necessary.
 Do not seek any clarification from invigilator.
1. For the C program below, select the correct output. ½M
int main() { a) 1
int i; b) 3
i = 1, 2, 3;
printf("%d", i);
c) 1, 2, 3
return 0; d) 3, 2, 1
}
2. What is the output of the following C program? ½M
int main() { a) error
int n; b) 3 2 1 0
for(n = 7; n!=0; n--)
printf("n = %d", n--);
c) infinite loop
return 0; d) 3 2 1 0
}
3. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)? ½M
a) for (i = n; i>0; i--)
b) for (i = n; i >= 0; i--)
c) for (i = n-1; i>0; i--)
d) for (i = n-1; i>-1; i--)
4. What is the output of the following C code? ½M
void main( ) { a) 9
int x = 12, z = 3; b) 1
int y = x >> 3;
printf(" %d\n", y);
c) 3
} d) 6
5. Give the output of the following C program. ½M
void main() { a) -4
int a = -5; b) -5
int k = (a++, ++a); c) 4
printf("%d\n", k); d) -3
}
6 What is the output of following C program? [Assume int takes 4 bytes] ½M
void main(void){ a) 4 5 9
int i, int arr[] = {1, 2, 3, 4, 5}; b) 1 2 3
for (i = 0; i < sizeof(arr)/arr[4]-1; ++i)
printf("%d\t", arr[i + 1]);
c) 2 3 4
} e) 3 4 5
7. What is the output of following C program? ½M
CSE 1051 Page 1 of 6
void main() { a) 2, 2, 0, 1
int i=-3, j=2, k=0, m; b) 1, 2, 1, 0
m = ++i || ++j && ++k; c) -2, 2, 0, 0
printf("%d, %d, %d, %d\n", i, j, k, m); d) -2, 2, 0, 1
}
8. What will be the final value of c in the following C code snippet for initial values of ½M
a = 1, b = 2, c = 1)
void main() { a) 0
int a=1, b=2, c=1; b) 1
c += (-c) ? a : b; c) 2
printf("%d",c); d) 3
}
9. Give the values of an array ‘a’ if we initializes it as follows: ½M
int a [8] = { -1, 0, 2, 0,1 };
a) -1, 0, 2, 0, 1, 2, 3, 4
b) 0, 0, 0, -1, 0, 2, 0, 1
c) -1, 0, 2, 0, 1, 1, 1, 1
d) -1, 0, 2, 0, 1, 0, 0, 0
10. What is the output of the following C code snippet? ½M
for(int i=0; i<20; i++) { a) 15 21
switch(i) { b) 16 21
case 0: i+=5; c) 16 20
case 1: i+=2; d) 15 20
case 5: i+=5;
default: i+=4;
break;
}
printf("%d ", i);
}
11. What are different bitwise operators in C? Explain. 1M

Bitwise Operators
 Bitwise Logical Operators
 Bitwise Shift Operators
 Ones Complement operator

Bitwise Logical Operators

& (AND), | (OR), ^ (EXOR)

These are binary operators and require two integer operands. These work on their
operands bit by bit starting from LSB (rightmost bit).

CSE 1051 Page 2 of 6


op1 op2 & | ^

1 1 1 1 0
1 0 0 1 1
0 1 0 1 1

0 0 0 0 0

Example:
Suppose x = 10, y = 15
z = x & y sets z=10 like this
0000000000001010  x x
0000000000001111  x y
0000000000001010  x z = x & y

Same way |,^ according to the table are computed.

Bitwise Shift operators


 << ,>>

These are used to move bit patterns either to the left or right. They are used in
the following
form op<<n or op>>n here op is the operand to be shifted and n is
number of positions to
shift.
 << causes all the bits in the operand op to be shifted to the left by n positions.
The leftmost n bits in the original bit pattern will be lost and the rightmost n
bits that are vacated are filled with 0’s
 >> causes all the bits in operand op to be shifted to the right by n positions.
The rightmost n bits will be lost and the left most vacated bits are filled with
0’s if number is unsigned integer.
 Suppose X is an unsigned integer whose bit pattern is 0000 0000 0000 1011
x<<1 0000 0000 0001 0110
x>>1 0000 0000 0000 0101
Example:
Suppose X is an unsigned integer whose bit pattern is 0000 0000 0000 1011
whose equivalent value in decimal number system is 11.
x<<3 0000 0000 0101 1000 = 88
x>>2 0000 0000 0000 0010 =2

Note:
x=y<<1; same as x=y*2 (Multiplication)
x=y>>1; same as x=y/2 (Division)

Bitwise Shift operators


Op and n can be constants or variables.

CSE 1051 Page 3 of 6


There are 2 restrictions on the value of n
n cannot be –ve. n should not be greater than number of bits used to represent Op.
(E.g.: suppose op is int and size is 2 bytes then n cannot be greater than 16).

Bitwise complement operator


The complement operator(~) is an unary operator and inverts all the bits
represented by its operand.
Suppose x=1001100010001111
~x=0110011101110000 (complement)
Also called as 1’s complement operator.
12. Differentiate between break and continue statements (any two points). 1M
break continue
1. A break can appear in A continue can appear only in loop
both switch and loop (for, while, do) statements.
(for, while, do) statements.
2. A break causes the switch or loop A continue doesn't terminate the
statements to terminate the loop, it causes the loop to go to the
moment it is executed. Loop next iteration. All iterations of the
or switch ends abruptly when loop are executed even
break is encountered. if continue is encountered.
The continue statement is used to
skip statements in the loop that
appear after the continue
3. A break causes the innermost A continue inside a loop nested
enclosing loop or switch to be within a switch causes the next
exited immediately. loop iteration.
[Any two differences – 0.5 Marks each]
13. Write a program in C to multiply an integer number N by 4 and divide an integer 2M
number M by 2 using bitwise operators only.
#include <stdio.h>
int main()
{
int m, n;
printf("Please enter value of m: ");
scanf("%d",&m);
printf("Please enter value of n: ");
scanf("%d",&n);
m=m<<2;
n=n>>1;
printf("\n After bit shift operations, the value of m= %d \n”,m);

CSE 1051 Page 4 of 6


printf("\n After bit shift operations, the value of n= %d \n”,n);
return(0);
}
14. What are the general rules for naming variables in C language? Explain with 2M
examples.
Rules for valid variable names (identifiers) :
• Name must begin with a letter or underscore ( _ ) and can be followed by any
combination of letters, underscores, or digits.
• Key words cannot be used as a variable name.
• C is case-sensitive: sum, Sum, and SUM each refer to a different variable.
• Variable names can be as long as you want, although only the first 63 (or 31)
characters might be significant.
• Choice of meaningful variable names can increase the readability of a program
Examples of valid variable names:
1) Sum 2) _difference 3) a
4) J5x7 5) Number_of_moves

Examples of invalid variable names:


1) sum$value 2) 3val 3) int
15. Write a neat flowchart to display prime numbers between integers X and Y. 2M

CSE 1051 Page 5 of 6


16 Write a simple program in C to accept each one of the following type of variables 2M
from the keyboard. Display it with suitable C commands.
(i) unsigned short integer
(ii) double precision number
(iii) long integer
(iv) character

#include <stdio.h>
int main()
{
unsigned short int n1=8;
double n2=4.45;
long int n3=987654321;
char n4='M';
printf("\n unsigned short int: %x", n1);
printf("\n Double precission: %g",n2);
printf("\n Long integer: %ld",n3);
printf("\n Character: %c \n", n4);
return(0);
}

 BEST OF LUCK 

CSE 1051 Page 6 of 6

Você também pode gostar