Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Instructions
1. Make a proper assignment copy
2. Submit assignment within 2 weeks (Exactly Friday, 23rd August 2019)
3. Submit in hardcopy
4. Write both question and answer with proper format. Code must follow proper indentation with comments.
5. While writing output take care of all the spaces and newlines properly.
6. All incorrect answers need to be resubmitted with the next assignment. If not done, then the next assignment will not
be checked.
Write output of the code segment with reason. [Some questions have other instructions. Follow the instructions for that
question only]
1. #include<stdio.h>
int main(){
int a = 2, b = 2, c = 0, d = 2, m;
m = a++ && b++ && c++ || d++;
printf("%d %d %d %d %d",a, b, c, d, m);
return 0;
}
2. #include<stdio.h>
int main(){
int i = 5;
if(i == 3, 4)
printf("Hai");
else
printf("No Hai");
return 0;
}
3. #include<stdio.h>
int main(){
int a = 5;
a = 1, 2, 3;
printf("%d", a);
return 0;
}
4. #include<stdio.h>
int main(){
int a;
a = (1, 2, 3);
printf("%d", a);
return 0;
}
5. #include<stdio.h>
int main(){
switch(25){
case 25L:
printf("25L");
break;
case 25.0:
printf("25.0");
break;
default:
printf("Nothing");
break;
}
return 0;
}
6. #include<stdio.h>
int main(){
char str[] = "\0";
if(printf("%s",str))
printf("inside if block");
else
printf("inside else block");
return 0;
}
7.#include<stdio.h>
int main(){
int i = 5, j = 4;
if(!printf(""))
printf("%d %d", i, j);
else
printf("%d %d", i++, ++j);
return 0;
}
8. #include<stdio.h>
int main(){
if(sizeof('\0'))
printf("inside if block");
else
printf("inside else block");
return 0;
}
9. #include<stdio.h>
int main(){
char str[8] = "hello";
char str1[8] = "2hello";
if(str == str1)
printf("Strings are Equal");
else
printf("Not Equal");
return 0;
}
10. #include<stdio.h>
int main(){
char *str = {"2braces"};
char *str1 = {"2braces"};
if(*str == *str1)
printf("inside if block");
else
printf("inside else block");
return 0;
}
11. C program to print "NITS", "Silchar" . Use both if and else statement
12.#include<stdio.h>
int main(){
char str1[] = "zoho";
char str2[] = "zoho";
if(strcmp(str1, str2))
printf("Strings are not same");
else
printf("Strings are Same");
return 0;
}
13. #include<stdio.h>
int main(){
char temp;
char arr[10] = {1, 2, 3, 4, 5, 6, 9, 8};
temp = (arr + 1)[2];
printf("%d\n", temp);
return 0;
}
14.#include<stdio.h>
int main(){
int _ = 5;
int __=5;
int ___ = _ + __;
printf("%d",___);
return 0;
}
15. #include<stdio.h>
int main(){
int arr[1] = {2};
printf("%d", 0[arr]);
return 0;
}
16. #include<stdio.h>
int main(){
char *ptr = "helloworld";
printf(ptr + 4);
return 0;
}
17. #include<stdio.h>
int main(){
int arr[3], i = 0;
while(i < 3) {
arr[i] = ++i;
}
for(i=0; i<3; i++){
printf("%d--", arr[i]);
}
return 0;
}
18. #include<stdio.h>
int main(){
printf("%d",EOF);
return 0;
}
19. #include<stdio.h>
int main(){
char *ptr = "2braces.com";
printf("%c\n",*&*ptr);
return 0;
}
20. #include<stdio.h>
int main(){
int scanf = 13;
printf("%d",scanf);
int printf = 13;
int c = 7 + printf;
printf("%d",c);
return 0;
}
21. #include<stdio.h>
int main(){
int a[][] = {{1,2},{3,4}};
int i, j;
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
printf("%d ", a[i][j]);
return 0;
}
22.#include<stdio.h>
int main(){
int a, b, c;
char *p = 0;
int *q = 0;
double *r = 0;
a = (int)(p + 1);
b = (int)(q + 1);
c = (int)(r + 1);
printf("%d %d %d",a, b, c);
return 0;
}
25. #include<stdio.h>
int main(){
int main = 47;
printf("%d",main);
return 0;
}
26. #include<stdio.h>
int main(){
extern num;
printf("%d",num);
return 0;
}
int num = 36;
31. #include<stdio.h>
int main(){
printf("%d", sizeof(void *));
return 0;
}
32. #include<stdio.h>
int main(){
char arr[] = "Cat";
*arr = 'B';
printf("%s", arr);
return 0;
}
33. Which of the following are incorrect statements? int a=10 is a statement.
a) if( 10==a ) printf("IncludeHelp");
c) if( 10=a ) printf("IncludeHelp");
34. #include<stdio.h>
#define i 10
int main(){
#define i 20
printf("%d",i);
return 0;
}
35 #include<stdio.h>
void main(){
int x=1;
if(x--)
printf("The Godfather");
--x;
else
printf("%d",x);
}
36 #include<stdio.h>
#define clrscr() 50
int main(){
clrscr();
printf("%d\n",clrscr());
return 0;
}
37. #include<stdio.h>
int main(){
struct node {
int a, b, c;
};
struct node num = {3, 5, 6};
struct node *ptr = & num;
printf("%d\n", *((int*)ptr + 1 + (3-2)));
return 0;
}
38 #include<stdio.h>
#define CONDITION(i)
printf("preprocessor\n");
int main(){
CONDITION(0);
return 0;
}
39. #include<stdio.h>
void main(){
if('\0');
else if(NULL)
printf("cquestionbank");
Else;
}
41. #include<stdio.h>
int main(){
char array[5] = "Knot", *ptr, i, *ptr1;
ptr = &array[1];
ptr1 = ptr + 3;
*ptr1 = 101;
for(i = 0; i < 4;i++)
printf("%c", *ptr++);
return 0;
}
43. #include<stdio.h>
# define loop while(true)
int main(){
loop;
printf("preprocessor-aptitude");
return 0;
}
44. #include<stdio.h>
main(){
int a=2;
if(a-- , --a, a)
printf("Tom");
else
printf("Jerry");
}
45. #include<stdio.h>
#include<stdlib.h>
int main(){
int i, numbers[1];
numbers[0] = 9;
free(numbers);
printf("\nStored integers are ");
printf("\nnumbers[%d] = %d ", 0, numbers[0]);
return 0;
}
47. #include<stdio.h>
int main(){
int EOF = 0;
printf("%d", EOF);
return 0;
}
48. #include <stdio.h>
int main(){
int i=i++,j=j++,k=k++;
printf("%d%d%d",i,j,k);
return 0;
}
49.#include<stdio.h>
# define x --5
int main(){
printf("%d",x);
return 0;
}
51. #include<stdio.h>
int main(){
int i = 0, j = 1, k = 0;
if(++k, j, i++)
printf("%d %d %d", i, j, k);
return 0;
}
52. #include<stdio.h>
int main(){
int i;
if(true)
printf("This will work");
else
printf("This will not work");
return 0;
}
55. #include<stdio.h>
x() {
printf("Function");
}
y(){
printf("Function in C");
}
z(){
printf("C function");
}
main(){
int (*ptr[3])();
ptr[0] = x;
ptr[1] = y;
ptr[2] = z;
ptr[2]();
return 0;
}
56. #include<stdio.h>
int main(){
float me = 5.25;
double you = 5.25;
if(me == you)
printf("I love U");
else
break;
return 0;
}
59. #include<stdio.h>
void fun(int*, int);
void (*ptr[1])(int*, int);
int main(){
int a = 2;
int b = 4;
ptr[0] = fun;
ptr[0](&a, b);
printf("%d %d ", a, b);
return 0;
}
void fun(int *p, int q)
{
int tmp = *p;
*p = q;
q = tmp;
}
60. #include<stdio.h>
int main(){
EOF++;
printf("%d", EOF);
return 0;
}
61. Functions cannot return more than one value at a time. True/False?
62. Which of the following cannot be used as LHS of the expression in “for (exp1;exp2; exp3)”
a) Variable b) Function c) typedef d) macros
63. #include<stdio.h>
int main(){
int i = 65;
switch(i) {
case 65:
printf("Integer 65");
break;
case 'A':
printf("Char 65");
break;
default:
printf("Bye");
}
return 0;
}
65. #include<stdio.h>
int main(){
int i = 65;
char ch = 'B';
switch(ch, i) {
case 65:
printf("Integer");
break;
case 'B':
printf("Char");
break;
default:
printf("Bye");
}
return 0;
}
66. #include "stdio.h"
int main(){
int i = 1, j;
for ( ; ; );
{
if (i)
j = --i;
if (j < 10)
printf("C Programming", j++);
else
break;
}
return 0;
}
67. #include<stdio.h>
void fun(char**);
int main(){
char *arr[] = { "bat", "cat", "fat", "hat", "mat", "pat" };
fun(arr);
return 0;
}
void fun(char **p){
char *t;
t = (p += sizeof(int))[-1];
printf("%s\n", t);
}
68. #include<stdio.h>
int main(){
int arr[5][5][5] = {0};
int *b = arr;
int *c = arr + 1;
printf("%d", c - b);
return 0;
}
69. #include<stdio.h>
int main(){
int i = 1;
switch(i) {
case i:
printf("case 1 executed");
break;
case i + 1;
printf("case 2 executed");
break;
default:
printf("default block executed");
break;
}
return 0;
}
70. #include<stdio.h>
int main(){
int i = 0;
printf("Hello");
char s[4] = {'\b', '\t', '\r', '\n'};
for(i = 0;i<4;i++){
printf("%c", s[i]);
}
return 0;
}
72. #include<stdio.h>
int main(){
char s[] = {'a', 'b', 'c', '\n', 'c', '\0'};
char *p, *str, *str1;
p = &s[3];
str = p;
str1 = s;
printf("%d", ++*p + ++*str1-32);
return 0;
}
73. #include <stdio.h>
int main(){
char i = 0;
for (; i++; printf("%d", i));
printf("%d", i);
return 0;
}
74. #include<stdio.h>
int i;
int main(){
int t;
for (t=4;scanf("%d",&i)-t;printf("%d\n",i))
printf("%d--", t--);
return 0;
}
75. #include<stdio.h>
int main(){
static int a[ ] = {0, 1, 2, 3, 4};
int *p[ ] = {a, a + 1, a + 2, a + 3, a + 4};
int **ptr = p;
++*ptr;
printf("%d %d %d", ptr - p, *ptr - a, **ptr);
return 0;
}
76. #include<stdio.h>
int main(){
char *str1 = "First";
char *str2 = "Second";
switch(*str1){
case "First":
printf("USAIN BOLT");
case "Second":
printf("JUSTIN GATLIN");
break;
default:
printf("Others");
}
return 0;
}
77. #include<stdio.h>
int main(){
int a, b, c;
int arr[5] = {1, 2, 3, 25, 7};
a = ++arr[1];
b = arr[1]++;
c = arr[a++];
printf("%d--%d--%d", a, b, c);
return 0;
}
78.
#include<stdio.h>
int main(){
int i = 4;
while(i == 4--)
printf("Loop ");
return 0;
}
79. #include<stdio.h>
int main(){
static char *arr[ ] = {"bike", "bus", "car", "van"};
char **ptr[ ] = {arr+3, arr+2, arr+1, arr};
char ***p;
p = ptr;
**++p;
printf("%s",*--*++p + 2);
}
80. #include<stdio.h>
#define N 1.5
int main(){
int i = 6;
switch(i){
case N + 5:
printf("Switch ON the fan");
break;
case N * 4:
printf("Switch ON the Air Cooler");
break;
default:
printf("Save Energy");
}
return 0;
}
81. #include<stdio.h>
int main(){
char ch = '\0';
switch(ch){
case NULL:
printf("Empty \0");
break;
case ' ':
printf("Empty Empty");
break;
case '0':
printf("Empty 0");
break;
default:
printf("Nothing");
}
return 0;
}
82. #include<stdio.h>
int main(){
int arr[3], i = 0;
while(i < 3){
arr[i] = ++i;
}
for(i=0; i<3; i++){
printf("%d--", arr[i]);
}
return 0;
}
83. #include<stdio.h>
#define loop for(;;)
int main(){
printf("DONE");
loop;
return 0;
}
84.
#include<stdio.h>
int main(){
while(!!7)
printf("Hai");
return 0;
}
85. #include<stdio.h>
int main(){
char ch = 96.0;
switch(ch){
case 96:
printf("inside case");
break;
default:
printf("inside default");
}
return 0;
}
86. #include<stdio.h>
int main(){
short int si = 1;
switch(++si - si++){
case 1L:
printf("First");
break;
case 2L:
printf("Second");
break;
default:
printf("Bye");
break;
}
return 0;
}
87. #include<stdio.h>
int main(){
char s[ ] = "d";
int i;
for(i = 0;s[ i ];i++)
printf("%c %c %c %c",s[ i ], *(s+i), *(i+s), i[s]);
return 0;
}
93. #include<stdio.h>
int main(){
int i = 5;
while(--i > 0)
printf("Loop ");
return 0;
}
94. #include<stdio.h>
int main(){
int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
printf("%d",sizeof(x));
return 0;
}
96. #include<stdio.h>
int main(){
unsigned int num = -4;
printf("%d", ~num);
return 0;
}
97. Write a C program to keep calculating the sum of the digits of a number until the number is a single digit.
101. Write a C program to check whether number is EVEN or ODD, without using any arithmetic or relational operators.
104. What error occurs in the absence of an exit condition in a recursive function?
106. What symbol is used to Left-justify within the data given field width?
107. Which of the following operators is used to concatenate two strings without space?
108. #include <stdio.h>
void (*(f)())(int, float);
void (*(*x)())(int, float) = f;
void ((*y)(int, float));
void foo(int i, float f);
int main(){
y = x();
y(1, 2);
}
109. Write a C program to print your name 10 times without using any loop or goto statement.
110. int extern x;
void main()
printf("%d",x);
x=2;
getch();
}
int x=23;
111. What specifies the minimum number of characters to print after being padded with zeros or blank spaces?
112. Garbage collector frees the programmer from worrying about ___________.
114. In the function malloc() and calloc, each byte of allocated space is initialized by what value?
118. If the space in memory allocated by malloc is not sufficient, then an allocation fails and returns ___________
120. If memory leak occurs, it is mandatory that a dangling pointer arises. True/False?
121. A function is declared as sqrt(-x) under the header file math.h, what will the function return?
126. When compiler accepts the request to use the variable as a register, where the value of the variable is stored?
132. #include<stdio.h>
enum sanfoundry{
a,b,c
};
enum sanfoundry g;
main(){
//g++;
printf("%d",g);
}
135. Which among the following is never possible in C when members in a structure are same as that in a union?
Assume I)Let X be a structure II)Let Y be a union
a) sizeof(X) is greater than sizeof(Y)
b) sizeof(X) is equal to sizeof(Y)
c) sizeof(X) is less than to sizeof(Y)
d) none of the mentioned
149. Which of the following should be used for freeing the memory allocated in the following C code?
#include <stdio.h>
struct pi{
struct pi *next;
int z;
};
int main(){
struct pi *p = (struct pi*)malloc(sizeof(struct pi));
p->z = 1;
p->next = (struct pi*)malloc(sizeof(struct pi));
return 0;
}
a)free(p); free(pp->next)
b)free(p->next);free(p);
c) free(p);
d) all of the mentioned
In the following questions write C programs with proper indentation and comments.
160. A program that has no command line arguments will have argc no values. True/False
161. Replace all 0’s with 1 in a given integer
162. Binary to decimal conversion
163. Program to find all the patterns of 0(1+)0 in the given string (0(1+)0 – There should be at least one ‘1’ between the two 0’s. Eg.
I/P: 01101111010 O/P: 3)
164. Program to count the common sub sequence of two strings(Eg. I/P: string 1 = “abcd”
string 2 = “abc” O/P: Number of common sub-sequences = 7)
165. Program to find all triplets with the given sum in the given array(Eg. Consider the array: arr[] = {0, -1, 2, -3, 1}. The given
sum is -2. In the given array, the triplets with sum = -2 are {0, -3, 1} and {-1, 2, -3}. I/P:0, -1, 2, -3, 1 O/P:{0, -3, 1} and {-1, 2, -
3})
166. Program to find all symmetric pairs in an array(Eg. I/P:arr [6] [2] = {{1, 2}, {3, 4}, {5, 6}, {2, 1}, {4, 3},{10,11}} O/P:{1,2}
and {2,1}, {3,4} abd {4,3})
167. Program to find the maximum product subarray in a given array (Eg. I/P: {-1, -3, -10, 60,0}
O/P: {-3, -10, 60} Product of the sub-array = 1800)
168. Program to find the equilibrium index of an array (Equilibrium index of an array is an index such that Sum of elements at
lower indexes = Sum of elements at higher indexes.Eg. I/P:a[] = {-7, 1, 5, 2, -4, 3, 0} O/P: a[0] + a[1] + a[2] = a[4] + a[5] + a[6]
Equilibrium index= 3 )
169. Printing an array into Zigzag fashion (In a zig-zag pattern, the first integer is less than the second integer, which is greater than
the third integer, which is less than the fourth integer, and so on. Hence, the converted array should be in the form of e1 < e2 > e3 <
e4 > e5 < e 6.I/P: 7 4 3 7 8 6 2 1O/P: 3 7 4 8 2 6 1)
170. Program to find the saddle point coordinates in a given matrix (A saddle point is an element of the matrix, which is the
minimum element in its row and the maximum in its column. Eg. I/P: {{1, 2 , 3 },{4,5,6},{7,8,9}} O/P: 7)
171. Program to print the elements of a 2d array in the form of a matrix in spiral form (I/P: 1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
O/P: 1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11)