C Programming

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

How to achieve the same functionality as calloc() by using malloc() folowed by memset()?

ptr = malloc(size); memset(ptr, 0, size);

Allocate memory for array of 5 integers

// C program to demonstrate the use of calloc() // and malloc() #include <stdio.h> #include <stdlib.h> int main() { int* arr; // malloc() allocate the memory for 5 integers // containing garbage values arr = (int*)malloc(5 * sizeof(int)); // 5*4bytes = 20 bytes // Deallocates memory previously allocated by malloc() function free(arr); // calloc() allocate the memory for 5 integers and // set 0 to all of them arr = (int*)calloc(5, sizeof(int)); // Deallocates memory previously allocated by calloc() function free(arr); return (0); }

What is the output? #include <stdio.h> main() { char c = 'A'+255; printf("%c", c); } Choices: A B Overflow error at runtime Compile error

A

Properties of calloc

Allocates space for an array with nmemb elements, each of which is size bytes long. Returns a null pointer if the requested space isn't available. Initializes allocated memory by setting all bits to 0.

What does realloc do?

Resizes a previously allocated block of memory

Call calloc and allocate space for an array of n integers

a = calloc(n, sizeof(int));

What does calloc do?

Allocates a block of memory and clears it

What is the output? #include <stdio.h> main() { int x = 5; if(x==5) { if(x==5) break; printf("Hello"); } printf("Hi"); } Choices: Compile error Hi HelloHi Hello

Compile error

How to open, read or write, and close a file

//Opens a text file for reading FILE *fp; fp = open("test.txt", "r'); //reads up to n-1 characters from the input stream reference by file pointer char buff[255]; //n=255 fgets(buff, 255, (FILE*) fp); printf("%s\n", buff); //Writes the stream to the output stream referenced by file pointer fputs("test.txt", fp); //Closes file fclose(fp);

Is the following if statement legal? if (n == 1-10) printf("n is between 1 and 10\n");

legal ( n == -9 is conditional statement)

What does the function do? unsigned int u; printf("%o", u); Choices: reads u in base 10 writes u in base 10 reads u in base 8 writes u in base 8 reads u in base 16 writes u in base 16

writes u in base 8

Given the code below. What is the content of each variable after the code is executed? int x =1, y, *p = &y, *q = &x; And the memory table Variable Address x 0xcc y 0xc8 p 0xc0 q 0xb8

x = 1, y = 0, p = 0xc8, q = 0xcc

What is the output? #include <stdio.h> int main(void) { int i; floatx; i = 40; x= 839.21f; printf("|%10.3f|", x): return 0; } Choices: | 839.210| | 8.392e+02| |839.21 |

| 839.210|

What is wrong with the following statements? char *p = malloc(4); ... free(p); ... strcpy(p, "abc"); //WRONG, HOW?

p points to the location of freed memory. This memory will be reallocated to others in future. Working with p will corrupt data of other variables.

What is an alternative to writing (*p).i = 10?

p->i = 10;

When calling a member of struct pointer why does *p.a =10 not work ?

* p has higher precedence over .

Which of the following is a logical NOT operator? Choices: ! && & All of the above

!

What is the output? #include <stdio.h> #define sqr(i) i*i main() { printf("%d %d", sqr(3), sqr(3+1)); } Choices: 9 16 9 7 Error: macro cannot be defined in lower case. None of the above.

9 7 (The equivalent expansion is -> printf("%d %d", 3*3, 3+1*3+1);)

What is the output? #include <stdio.h> void main() { float x = 3.1412; int z = x - (int) x; printf("%d", z); } Choices: Nothing, since there is an error in the program 0.141200 0 Nothing, since there is a warning in the program

0

What is the output? #include <stdio.h> main() { printf("%d",strcmp("strcmp()","strcmp()")); } Choices: 0 1 -1 Invalid use of stcmp() function

0

What is the output of the following program? #include "stdio.h" void main () { printf("10.1f", 839.21354f); }

839.2

Which of the following is true? Choices "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); memset(ptr, 0, m * n); "ptr = calloc(m, n)" is equivalent to following ptr = malloc(m); memset(ptr, 0, m); "ptr = calloc(m, n)" is equivalent to following ptr = malloc(n); memset(ptr, 0, n);

"ptr = calloc(m, n)" is equivalent to following ptr = malloc(m * n); memset(ptr, 0, m * n);

How to obtain the arguments from command line

#include <stdio.h> int main(int argc, char*argv[]) { printf("Program name %s\n", argv[0]); if(argc == 2) { printf("The argument supplied is %s\n", argv[1]); } else if(argc > 2) { printf("Too many arguments supplied.\n"); } }

Write code asking user to input the numerator and denominator values from keyboard in the following format: num/denom For example: Input fractional number a/b: 3/4 The input fractional number is 0.750000

#include <studio.h> int main(){ float num, denom; float decimal; printf("Input fractional number a/b"); scanf("%f/%f", &num, &denom); decimal = num/denom; printf("The input fractional number is %lf", decimal); return 0; }

What is the output? #include<stdio.h> void swap (char *x, char *y) { char *t = x; x = y; y = t; } int main() { char *x = "geeksquiz"; char *y = "geeksforgeeks"; char *t; swap(x, y); printf("(%s, %s)", x, y); t = x; x = y; y = t; printf("\n(%s, %s)", x, y); return 0; } Choices: (geeksquiz, geeksforgeeks) (geeksforgeeks, geeksquiz) (geeksforgeeks, geeksquiz) (geeksquiz, geeksforgeeks) (geeksquiz, geeksforgeeks) (geeksquiz, geeksforgeeks) (geeksforgeeks, geeksquiz) (geeksforgeeks, geeksquiz)

(geeksquiz, geeksforgeeks) (geeksforgeeks, geeksquiz)

Consider the following declaration : struct addr { char city[10]; char street[30]; int pin ; }; struct { char name[30]; int gender; struct addr locate; } person , *kd = &person ; Then *(kd -> name +2) can be used instead of Choices: person.name +2 kd -> (name +2 ) *((*kd).name + 2 ) either (A) or (B), but not (C)

*((*kd).name + 2 ) (*(kd -> name +2) = *((*kd).name + 2 ))

int x = ~1; What is the value of 'x'? Choices: 1 -1 2 -2

-2

Write the following functions, assuming that the date structure contains three members: b) int compare_dates(struct date d1, struct date d2); Returns -1 if d1 is an earlier date than d2, +1 if d1 is a later date than d2, and () if d1 and d2 are the same.

//define the date structure struct date { //member variables int month, day, years; }; //function declaration int compare_dates(struct date d1, struct date d2); //compares two dates and returns corresponding values -1,0,1 int compare_dates(struct date d1, struct date d2) { int value; value = d1.year - d2.year; //compare two years if(value == 0) { value = d1.month - d2.month; //compare two months if(value == 0) //compare two days value = d1.day- d2.day; } if (value <0) value = -1; else if(value == 0) value = 0; else value = 1; return value; }

Write the following functions, assuming that the date structure contains three members: a) int day_of_year (struct date d); Returns the day of the year (an integer between 1 and 366) that corresponds to the date d. b) int compare_dates(struct date d1, struct date d2); Returns -1 if d1 is an earlier dat than d2, +1 if d1 is a later date than d2, and () if d1 and d2 are the same.

//define the date structure struct date { //member variables int month, day, years; }; //function declaration int date_of_year(struct date d); int daysOfMonth[] [12] = { //normal years, non-leap years 31,28,31,30,31,30,31,31,30,31,30,31, //leap year 31,29,31,30,31,30,31,31,30,31,30,31, } //check the year is leap year or not and returns number of days int day_of_year(struct date d) { int days, leap, i; //determine wheter it is a leap year leap = ((d.year % 4 ==0 && d.year % 100 != 0) || d.year % 400 == 0) ? 1 : 0; for( i = 0, days = 0; i < d.month - 1; i++) days += daysOfMonth[leap][i]; days += d.day; return days; }

Write a function named my_malloc that serves as a "wrapper" for malloc. When we call my_malloc and ask it to allocate n bytes, it in turn calls malloc, tests to make sure that malloc doesn't return a null porinter, and then returns the pointer from malloc. Have my_malloc print an error message and terminate the program if malloc returns a null pointer.

//function my_malloc void *may_malloc(size_t n) { void *p; p = malloc(n); //checking condition for null pointer if(p == NULL) { //prints error message printf("Memory allocation failed\n"); } //returns the pointer return p; }

What is the value of 'y'? #include <stdio.h> main() { int x = 1; float y = x>>2; printf( "%f", y ); } Choices: 4 0.5 0 1

0

What is the output? i = 2; j = 1; k = 0; i *= j *= k; printf("%d %d %d", i, j, k);

0 0 0

What is the output? #include<stdio.h> void f(int *p, int *q) { p = q; *p = 2; } int i = 0, j = 1; int main() { f(&i, &j); printf("%d %d n", i, j); getchar(); return 0; } Choices: 2 2 2 1 0 1 0 2

0 2

What is the output? i = 7; j = 8; k = 9; printf("%d ", i - 7 && j++ < k); printf("%d %d %d", i, j, k);

0 7 8 9

Which one of the following is not a legal way to write the number 65? (Assume that the character set is ASCII) Choices: 'A' 0b1000001 0101 0x41

0b1000001 (has value of 48)

What is the value of the variable y after this code is executed? #include <stdio.h> void main() { unsigned short x = 0xcdab; unsigned short y = x << 2; } Choices: 0xaf34 0x36ac 0x2af34 0x1af34

0x36ac

What is the value of the variable y after this code is executed? #include <stdio.h> void main() { unsigned char x = 3; unsingend char y = ~x; } Choices: 0xfc -3 0xffc 0xc

0xfc

What is the output? #include <stdio.h> main() { int a[3] = {2,1}; printf("%d", a[a[1]]); } Choices: 0 1 2 3

1

What is the output? i = 1; j = 2; k = 3; printf("%d", i < j || k);

1

What is the output? i = 3; j = 2; k = 1; printf("%d", i < j == j < k);

1

What is the output? i = 5; j = 10; k = 1; printf("%d", k > i < j);

1

What will be the output produced by the following C code: int main() { int array[5][5]; printf("%d",( (array == *array) && (*array == array[0]) )); return 0; } 1 0 2 -1

1

What is the output? i = 5; j = 0; k = -5; printf("%d", i && j || k);

1 ( i && j || k == 5 && 0 || -5 == 0 || -5 == 1)

What is output ? i = 2; j = 1; printf("%d", !!i + !j);

1 (!!i + !j == !(!2) + !1 == !(0) + 0 == 1 + 0 == 1)

What is the output? i = 10; j = 5; printf("%d", !i < j);

1 (!(i <j) == !(0) == 1)

What is the output? i = 2; j = 3; k = i * j == 6; printf("%d", k);

1 (i * j first then compare)

What is the output? i = 1; j = 1; k = 1; printf("%d ", ++i || ++j && ++k); printf("%d %d %d", i, j, k);

1 2 1 1 ( since ++i is left, i is incremented, j and k are not will short circuit)

What is the output? i = 1; while (i <= 128) { printf("%d ", i); i *= 2; }

1 2 4 8 16 32 64 128

What is the output? i = 3; j = 4; k = 5; printf("%d " , i < j || ++j < k); printf("%d %d %d", i, j, k);

1 3 4 5 (++j will short circuit compiler)

What is the output? i = 7; j = 8; k = 9; printf("%d ", ( i = j) || (j = k)); printf("%d %d %d", i, j, k);

1 8 8 9

What is the output? #include <stdio.h> struct rec { int i; float f; char c; }; int main() { struct rec *p; p=(struct rec *) malloc (sizeof(struct rec)); (*p).i=10; (*p).f=3.14; (*p).c='a'; printf("%d %f %c\n",(*p).i,(*p).f,(*p).c); free(p); return 0; }

10 3.140000 a

Give the decimal value of each of the following integer constant. 0x77

119 (hexadecimal)

What is the out put? i = 6; j = i += i; printf("%d %d", i, j);

12 12

What is the output? #include <stdio.h> #define print(x) printf("%d ", x) int x; void Q(int z) { z += x; print(z); } void P(int *y) { int x = *y + 2; Q(x); *y = x - 1; print(x); } main(void) { x = 5; P(&x); print(x); } Choices: 12 7 6 22 12 11 14 6 6 7 6 6

12 7 6

Suppose that we call scanf as follows: scanf("%f%d%f", &x, &i, &y); If the user enters: 12.3 45.6 789 what will be the values of x, i, and y after the call?

12.3 45 .6

Consider the following C declaration struct { short s[5]; union { float y; long z; }u; } t; Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is (GATE CS 2000) Choices: 22 bytes 14 bytes 18 bytes 10 bytes

18 bytes (Short array s[5] will take 10 bytes as size of short is 2 bytes. When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8))

What is the output? #include <stdio.h> main() { int a[] = {2,1}; printf("%d", *a); } Choices: 0 1 2 Compile error

2

Predict the output of following C program #include<stdio.h> struct Point{ int x, y, z; }; int main(){ struct Point p1 = {.y = 0, .z = 1, .x = 2}; printf("%d %d %d", p1.x, p1.y, p1.z); return 0;} Choices: Compiler Error 2 0 1 0 1 2 2 1 0

2 0 1

What is the output? #include <stdio.h> main() { int i = 1; while(++i){ printf("%d ", i++); } Choices : 1 3 5 2 4 2 4 6 2

2 4

What is the output? #include <stdio.h> main() { int i = 1; while( i++<=5 ) printf("%d ",i++); } Choices: 1 3 5 2 4 2 4 6 2

2 4 6

Given the code below. What is the value of x?: int x = 3, *p. *q, *r; p = &x; q = p; *&r = *&q; r = 2 Choices: 9 *&q, which is the value of the pointer q 3 2

3

What is the output? #include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int *p = arr; ++*p; p += 2; printf("%d", *p); return 0; } Choices: 2 3 4 Compiler Error

3

What is the output of the following program? #include <stdio.h> void f() { static int i = 3; printf("%d ", i); if(--i) f(); } main() { f(); } Choices: 3 2 1 0 3 2 1 3 3 3 Compile error

3 2 1

What is the output? i = j = k = 1; i += j += k; printf("%d %d %d", i, j, k);

3 2 1 ( i = 3, j = 2, k =1)

What is the output? i = 5; j = ( i-= 2) +1; printf("%d %d", i, j);

3 4

What is the size of the following union definition? #include <stdio.h> union abc { char a,b,c,d,e,f,g,h; int i; }abc; main() { printf( "%d", sizeof( abc )); } Choices: 1 2 4 8

4

Assume int is 4 bytes, char is 1 byte and float is 4 bytes. Also, assume that pointer size is 4 bytes (i.e. typical case)char *pChar;int *pInt;float *pFloat; sizeof(pChar);sizeof(pInt);sizeof(pFloat); What's the size returned for each of sizeof() operator? Choices: 4 4 4 1 4 4 1 4 8 None of the above

4 4 4

Consider the C program below. What does it print? # include <stdio.h> # define swapl (a, b) tmp = a; a = b; b = tmp void swap2 ( int a, int b){ int tmp; tmp = a; a = b; b = tmp; } void swap3 (int*a, int*b){ int tmp; tmp = *a; *a = *b; *b = tmp; } int main (){ int num1 = 5, num2 = 4, tmp; if (num1 < num2) {swap1 (num1, num2);} if (num1 < num2) {swap2 (num1 + 1, num2);} if (num1 >= num2) {swap3 (&num1, &num2);} printf ("%d, %d", num1, num2);} /* Add code here. Remove these lines if not writing code */ Choices: 5, 5 5, 4 4, 5 4, 4

4, 5

What is the output? #include <stdio.h> main() { unsigned x = 5, y=&x, *p = y+0; printf("%u",*p); } Choices: Address of x Address of y Address of p 5

5

What is the output? #include<stdio.h> main() { struct { int x;} var = {5}, *p = &var; printf("%d %d %d",var.x,p->x,(*p).x); } Choices: 5 5 5 5 5 garbage value 5 5 0 Compile error

5 5 5

What is the output? i = 5; j = 3 - 2 * i++; printf("%d %d", i, j);

6 -7

What is the output? #include <stdio.h> main() { int i = 13, j = 60; i ^= j; j ^= i; i ^= j; printf("%d %d", i, j); } Choices: 73 73 60 13 13 60 60 60

60 13

Give the decimal value of each of the following integer constant. 077

63 (octal)

What is the output? i = 7; j = 8; i *= j+1; printf("%d %d", i, j);

63 8 (i = 63, j = 8)

Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed. union test { int x; char arr[8]; int y; }; int main() { printf("%d", sizeof(union test)); return 0; } Choices: 12 16 8 Compiler Error

8

What is the output? i = 7; j = 6 + (i = 2.5); printf("%d %d", i, j);

8 2

What is the value of q after the code is executed? int *p, q = 3; p = &q; *p = *p * *p; Choices: 3 Undefined 9 &q, which is the address of q

9

What is the output? #include <stdio.h> main() { int x = 65, *p = &x; void *q=p; char *r=q; printf("%c",*r); } Choices: Garbage character A 65 Compile error

A

What is the output? #include <stdio.h> #include <stdlib.h> #include <string.h> struf("Hello, World!\n");ct COORD { int x; int y; }; int main() { char* p = malloc(8); struct COORD* q = p; q->x = 65 + (67 << 8); q->y = 66; printf("%s\n", p); return 0; }

AC

Assume that size of an integer is 32 bit. What is the output of following program? #include<stdio.h> struct st{ int x; static int y;}; int main() { printf("%d", sizeof(struct st)); return 0; } Choices: 4 8 Compiler Error Runtime Error

Compiler Error (In C, struct and union types cannot have static members. In C++, struct types are allowed to have static members, but union cannot have static members in C++ also.)

What is the output? #include‹stdio.h› int main() { struct site { char name[] = "GeeksQuiz"; int no_of_pages = 200; }; struct site *ptr; printf("%d ", ptr->no_of_pages); printf("%s", ptr->name); getchar(); return 0; } Choices: 200 GeeksQuiz 200 Runtime Error Compiler Error

Compiler Error (When we declare a structure or union, we actually declare a new data type suitable for our purpose. So we cannot initialize values as it is not a variable declaration but a data type declaration.)

Anyone of the followings can be used to declare a node for a singly linked list. If we use the first declaration, "struct node * nodePtr;" would be used to declare pointer to a node. If we use the second declaration, "NODEPTR nodePtr;" can be used to declare pointer to a node. /* First declaration */ struct node {int data;struct node * nextPtr;}; /* Second declaration */typedef struct node{int data;NODEPTR nextPtr;} * NODEPTR; Choices: TRUE FALSE

FALSE (The typedef usage is incorrect. Basically, we can't use yet to be typedef-ed data type inside while applying typedef itself. Here, NODEPTR is yet to be defined (i.e. typedef-ed) and we are using NODEPTR inside the struct itself.)

Pick the best statement for the below program: #include "stdio.h" int main(){ struct {int i; char c;} myVar = {.c ='A',.i = 100}; printf("%d %c",myVar.i, myVar.c); return 0;} Choices Compile error because struct type (containing two fields of dissimilar type i.e. an int and a char) has been mentioned along with definition of myVar of that struct type Compile error because of incorrect syntax of initialization of myVar. Basically, member of operator (i.e. dot .) has been used without myVar Compile error for not only B but for incorrect order of fields in myVar i.e. field c has been initialized first and then field i has been initialized No compile error and it'll print 100 A

No compile error and it'll print 100 A (As per C language, initialization of a variable of complete data type can be done at the time of definition itself. Also, struct fields/members can be initialized out of order using field name and using dot operator without myVar is ok as per C)

Pick the best statement for the below program: #include "stdio.h" int main(){ struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2}; printf("%d %d %d and",arr[0].a[0],arr[0].a[1],arr[0].b); printf("%d %d %dn",arr[1].a[0],arr[1].a[1],arr[1].b); return 0; } Choices: Compile error because struct type (containing two fields i.e. an array of int and an int) has been specified along with the definition of array arr[] of this struct type. Compile error because of incorrect syntax for initialization of array arr[]. No compile error and two elements of arr[] would be defined and initialized. Output would be "1 0 1 and 2 0 2". No compile error and two elements of arr[] would be defined and initialized. Output would be "1 X 1 and 2 X 2" where X is some garbage random number.

No compile error and two elements of arr[] would be defined and initialized. Output would be "1 0 1 and 2 0 2" (In C, designators can be used to provide explicit initialization. For an array, elements which aren't initialized explicitly in program are set as ZERO)

What is your comment on the below C statement? signed int *p=(int*)malloc(sizeof(unsigned int)); Choices: Improper type casting Would throw Runtime error Memory will be allocated but cannot hold an int value in the memory No issue with statement

No issue with statement

#include<stdio.h> int main() { int arr[] = {10, 20, 30, 40, 50, 60}; int *ptr1 = arr; int *ptr2 = arr + 5; printf("Number of elements between two pointer are: %d.", (ptr2 - ptr1)); printf("Number of bytes between two pointers are: %d", (char*)ptr2 - (char*) ptr1); return 0; } Assume that an int variable takes 4 bytes and a char variable takes 1 byte Choices Number of elements between two pointer are: 5. Number of bytes between two pointers are: 20 Number of elements between two pointer are: 20. Number of bytes between two pointers are: 20 Number of elements between two pointer are: 5. Number of bytes between two pointers are: 5 Compiler Error Runtime Error

Number of elements between two pointer are: 5. Number of bytes between two pointers are: 20

Does both the loops in the following programs prints the correct string length? #include <stdio.h> main() { int i; char s[] = "hello"; for(i=0; s[i]; ++i); printf("%d ", i); i=0; while(s[i++]); printf("%d ", i); } Choices: Yes, both the loops prints the correct length Only for loop prints the correct length Only while loop prints the correct length Compile error in the program.

Only for loop prints the correct length

In the following program snippet, both s1 and s2 would be variables of structure type defined as below and there won't be any compilation issue. typedef struct Student { int rollno; int total; } Student; Student s1; struct Student s2; Choices: TRUE FALSE

TRUE (At first, it may seem that having same 'struct tag name' and 'typedef name' would cause issue here. But it's perfectly fine for both of them having same name. s1 is defined using typedef name Student while s2 is defined using struct tag name Student)

Which languages necessarily need heap allocation in the run time environment? Choices Those that support recursion Those that use dynamic scoping Those that use global variables Those that allow dynamic data structures

Those that allow dynamic data structures

The most appropriate matching for the following pairs (GATE CS 2000) X: m=malloc(5); m= NULL; 1: using dangling pointers Y: free(n); n->value=5; 2: using uninitialized pointers Z: char *p; *p = 'a'; 3. lost memory is: Choices: X—1 Y—3 Z-2 X—2 Y—1 Z-3 X—3 Y—2 Z-1 X—3 Y—1 Z-2

X—3 Y—1 Z-2 (X -> A pointer is assigned to NULL without freeing memory so a clear example of memory leak Y -> Trying to retrieve value after freeing it so dangling pointer. Z -> Using uninitialized pointers)

Given the function cal below int cal(int a[], int N, int i) { return i < N ? a[i] + cal(a, 5, i + 1) : 0; } Describe what does cal do when this code is execute: int a[5] = {1,2,3,4,5}; cal(a,5,0); Show the stack call including values of input parameter a, and i.

cal function sums up the values of array a. The function is comparing parameter i to N if i less than N than call cal function with parameter a, N, and i + 1. I i eqauls N return 0. cal(a,5,0) return 1 + cal(a,5,1) cal(a,5,1) return 2 + cal(a,5,2) cal(a,5,2) return 3 + cal(a,5,3) cal(a,5,3) return 4 + cal(a,5,4) cal(a,5,4) return 5 + cal(a,5,5) cal(a,5,5) return 0

For the following item of data, specify which one of the types char, short, int, or long is the smallest one guaranteed to be large enough to store the item. Days in a month

char ( range is 1-7, char is 7 bit, char range is 0-127 int)

Write a function named duplicate that uses dynamic storage allocation to create a copy of a string. For example, the call p = duplicate(str); would allocate space for a string of the same length as str, copy the contents of str into the new string, and return a pointer to it. Have duplicate return a null pointer if the memory allocation fails.

char *duplicate(const char *s) { char *temp = malloc(strlen(s) + 1); //check condition for null pointer if (temp == NULL) return NULL; //copy contents of str into the new string strcpy(temp, s); //returns the pointer return temp; }

string copy algorithm

char *string (char* string1, char *string2) { int i ; for( i = 0; stringq[i] != '\0'; ++i) { string2[i] = string1[i]; } s2[i] = '\0'; return string2; }

How to store array of strings?

char string [ ] [n]; (first dimensions is blank to wallow any length, n is the number of strings, string variable name is arbitrary)

A(n) ________ loop is a good choice when you know how many times you want the loop to iterate in advance of entering the loop Choices: recursion call do while while for None of these

for

Reverse the one dimensional array a of any size int main() { int a = {1,2,3,4,5}, i = 0; //use for loop to reverse the array a: for(...) { ... } return 0; }

for(; i < sizeof(a)/sizeof(int)/2; i++) { int temp = a[i]; int j = sizeof(a)/sizeof(int) - 1 - i a[i] = a[j]; a[j] = temp; }

Reverse the 2D array a of any size: int main() { int a = {1,2,3}, {4,5,6}, {7,8,9}, i = 0; //use for loop to reverse the array a: ...... return 0; }

for(i = 0; i <3; i++ ){ temp = a[i][j]; a[i][j] = a[i][j+2]; a[i][j+2] = temp; }

What is the output? #include <stdio.h> void f(char**); int main() { char *argv[] = { "ab", "cd", "ef", "gh", "ij", "kl" }; f(argv); return 0; } void f(char **p) { char *t; t = (p += sizeof(int))[-1]; printf("%sn", t); } Choices: ab cd ef gh

gh

What is the output? #include<stdio.h> { int x = 1; switch(x) { default: printf("Hello"); case 1: printf("hi"); break; } } Choices: Hello hi Hellohi Compile error

hi

Suppose that we call scanf as follows: scanf("%d%s%d", &i, s, &j); If the user enters 12abc34 56def78 what ill be the values of i, s, and j after the call? (Assume that i and j are int variables and s is an array of cahracters.)

i = 12, s = abc34, j = 56

Consider the following program, where are i, j and k are stored in memory? int i; int main(){ int j; int *k = (int *) malloc (sizeof(int)); } Choices: i, j and *k are stored in stack segment i and j are stored in stack segment. *k is stored on heap. i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap. j is stored in BSS part of data segment, i is stored in stack segment. *k is stored on heap.

i is stored in BSS part of data segment, j is stored in stack segment. *k is stored on heap (i is global variable and it is uninitialized so it is stored on BSS part of Data Segment (http://en.wikipedia.org/wiki/.bss) j is local in main() so it is stored in stack frame (http://en.wikipedia.org/wiki/Call_stack) *k is dynamically allocated so it is stored on Heap Segment. See following article for more details)

Suppose that in a C program snippet, followings statements are used. i) sizeof(int); ii) sizeof(int*); iii) sizeof(int**); Assuming size of pointer is 4 bytes and size of int is also 4 bytes, pick the most correct answer from the given options. Choices: Only i) would compile successfully and it would return size as 4. i), ii) and iii) would compile successfully and size of each would be same i.e. 4 i), ii) and iii) would compile successfully but the size of each would be different and would be decided at run time. ii) and iii) would result in compile error but i) would compile and result in size as 4.

i), ii) and iii) would compile successfully and size of each would be same i.e. 4

Is the following if statement legal? if ( n > = 1 < = 10) printf("n is between 1 and 10\n");

illegal (n >= 1 && n <=10 is legal)

Write the following function: int * find_largest(int a[], int n); When passed an array a of length n, the function will returan a pointer to the array's largest element.

int *find_largest(int a[], int n) { //declaring variables int i, *max; //initializing variables based on condition max = &a[0]; //loop to find largest element for(i = 1; i < n; i++) //check current element is largest or not, changes if to largest element accordingly if(a[i] > *max) *max = a[i]; //return pointer of largest element return max; }

Show how to declare a tag named complex for a structure with two members, real and imaginary, of type double. Write a function named add_complex that adds the corresponding members of its arguments (both complex structures), then returns the result (another complex structure).

struct complex add_complex(struct complex c1, struct complex c2){ //declare the third structure variable struct complex c3; //add structure members c3.real = c1.real + c2.real; c3.imaginary = c1.imagniary +c2.imaginary; //return structure variable return c3; }

Show how to declare a tag named complex for a structure with two members, real and imaginary, of type double. Use the complex tag to declare variables named c1, c2, and c3.

struct complex c1, c2, c3;

Show how to declare a tag named complex for a structure with two members, real and imaginary, of type double. Write a function named make_complex that stores its two arguments (both of type double) in a complex structure, then returns the structure.

struct complex make_complex(double real, double imaginary) { struct complex c; c.real = real; c.imaginary = imaginary; return c; }

Show how to declare a tag named complex for a structure with two members, real and imaginary, of type double.

struct complex { //declare variables as structure members double real; double imaginary; }

Create a struct vstring that has members int and char array. Then allocate memory for vstring to create the length of the char array

struct vstring { int len; char chars[]; } int n; struct vstring *str = malloc(sizeof(struct vstring) + n); str->len = n;

Declare structure variables named c1, c2, and c3, each having members real and imaginary of type double. Modify the declaration so that c1's members initially have the values 0.0 and 1.0, while c2's members are 1.0 and 0.0 initially. (c3 is not initialized.)

struct { double real, imaginary; } c1 = {0.0, 1.0}, c2 = {1.0, 0.0}, c3;

Declare structure variables named c1, c2, and c3, each having members real and imaginary of type double.

struct { double real, imaginary; } c1, c2, c3;

Given the code below int x =2, *p = &x, *q = p; *p = *p + *q; And the following memory table: Variable Address x 0x00ac p 0x00a0 q 0x0098

x = 4, p = 0x00ac, q = 0x00ac

What is the output? #include <stdio.h> int main(void) { int i; floatx; i = 40; x= 839.21f; printf("|%-5d|",i): return 0; } Choices: |40| | 40| |40 | | 040|

|40 |

Consider the following variable declarations and definitions in C i) int var_9 = 1; ii) int 9_var = 2; iii) int _ = 3; Choices: Both i) and iii) are valid. Only i) is valid. Both i) and ii) are valid. All are valid.

Both i) and iii) are valid.

Consider the following two C lines int var1; extern int var2; Which of the following statements is correct: Both statements only declare variables, don't define them. First statement declares and defines var1, but second statement only declares var2 Both statements declare define variables var1 and var2

First statement declares and defines var1, but second statement only declares var2

Predict the output #include <stdio.h> int var = 20; int main() { int var = var; printf("%d ", var); return 0; } Choices: Garbage Value 20 Compiler Error

Garbage Value

Show how to declare a tag named complex by using a type named Complex with two members, real and imaginary, of type double. (a) b) Use the type named Complex to declare variables named c1, c2, and c3. c) Write a function named make_complex that stores its two arguments (both of type double) in a complex structure, then returns the structure. d) Write a function named add_complex that adds the corresponding members of its arguments (both complex structures), then returns the result (another complex structure).

(a) typedef struct { double real, imaginary; } Complex; (b) Complex c1, c2, c3; (c) Complex make_complex(double real, double imaginary) { Complex c; c.real = real; c.imaginary = imaginary; return c; } (d) Complex add_complex(Complex c1, Complex c2) { Complex c3; c3.real = c1.real + c2.real; c3.imaginary = c1.imaginary + c2.imaginary; return c3; }

What is the output of the below code snippet? #include <stdio.h> main() { printf("%d", -11%2); } Choices: 1 -1 5.5 5.5

-1

What is the output? #include<stdio.h> main() { short unsigned int i = 0; printf("%u\n", i--); } Choices: 0 Compile error 65535 32767

0

What is the output? i = 3; j = 4; k = 5; printf("%d", i % j + i < k);

0

What is the output? #include<stdio.h> main() { enum { india, is=7, GREAT }; printf("%d %d", india, GREAT); } Choices: 0 1 0 2 0 8 Compile error

0 8

What is the value of the variable y? #include <stdio.h. void main() { unsigned char x = 3; unsigned short y = -x; } Choices: -3 0xfc 0xffc 0xc 0xff

0xffc

Given the code below: int a[3] = {1,100}, *p = a, *q = p; What is the value of *p++ and *++q respectively? 100 and 100 100 and 1 1 and 100 1 and 1 &a[0] and &a[1] &a[1] and &a[2]

1 and 100

What is the output? #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char* p = malloc(40); strcpy(p, "abc"); printf("%d-%d-%d-%d\n", sizeof(*p), sizeof(p), sizeof(p), strlen(p)); return 0; }

1-8-8-3

If i hast the value 17. What does it print if i has the value -17? printf("%d\n", i >= 0 ? i : -i);

17, 17

What is the output? int f(int x, int *py, int **ppz) { int y, z; **ppz += 1; z = **ppz; *py += 2; y = *py; x += 3; return x + y + z; } void main() { int c, *b, **a; c = 4; b = &c; a = &b; printf("%d ", f(c, b, a)); return 0; } Choices: 18 19 21 22

19

What is the output? i = 1; j = 2; k = 3; i -= j -= k; printf("%d %d %d", i, j, k);

2 -1 3 ( i = 2, j= -1, k =3)

What is the output? #include <stdio.h> main() { int a = 1; float b = 1.3; double c; c = a + b; printf("%.2lf", c); } Choices: 2.30 2.3 Compile error 2.0

2.30

What is the output of following program? # include <stdio.h> void fun(int x) { x = 30; } int main() { int y = 20; fun(y); printf("%d", y); return 0; } Choices: 30 20 Compiler Error Runtime Error

20

Consider the following C program.#include<stdio.h>void mystery(int *ptra, int *ptrb) { int *temp; temp = ptrb; ptrb = ptra; ptra = temp;}int main() { int a=2016, b=0, c=4, d=42; mystery(&a, &b); if (a < c) mystery(&c, &a); mystery(&a, &d); printf("%dn", a);} The output of the program _____________ Note : This question was asked as Numerical Answer Type. Choices: 2016 0 4 8

2016

int main() { int x = 032; printf("%d", x); return 0; } Choices: 32 0 26 50

26

Give the decimal value of each of the following integer constant. 0xABC

2748 (hexadecimal)

The value printed by the following program is void f(int* p, int m){ m = m + 5; *p = *p + m; return; } void main(){ int i=5, j=10; f(&i, j); printf("%d", i+j); } 10 20 30 40

30

What is the output? for (i = 5, j = i -1; i > 0, j >0; --i, j = i -1) printf("%d ", i);

5 4 3 2

What is the output? i = 7; j = 3 + --i * 2; printf("%d %d", i, j);

6 15

What is the output? i = 5; j = ++i * 3 -2; printf("%d %d", i, j);

6 16

What is the output? i = 7; j = 3 * i-- + 2; printf("%d %d", i, j);

6 23

What is the output? i = 2; j = 8; j = (i = 6) + (j = 3); printf("%d %d", i, j);

6 9

Assume that float takes 4 bytes, predict the output of following program. #include <stdio.h> int main() { float arr[5] = {12.5, 10.0, 13.5, 90.5, 0.5}; float *ptr1 = &arr[0]; float *ptr2 = ptr1 + 3; printf("%f ", *ptr2); printf("%d", ptr2 - ptr1); return 0; } Choices: 90.500000 3 90.500000 12 10.000000 12 0.500000 3

90.500000 3 (When we add a value x to a pointer p, the value of the resultant expression is p + x*sizeof(*p) where sizeof(*p) means size of data type pointed by p. That is why ptr2 is incremented to point to arr[3] in the above code. Same rule applies for subtraction. Note that only integral values can be added or subtracted from a pointer. We can also subtract or compare two pointers of same type)

What is the output? i = 9384; do { printf("%d ", i); i /= 10; } while (i > 0);

9384 938 93 9

What is the c header file for Boolean?

<stdbool.h>

What is a pointer? Choices: A keyword used to create variables A variable used to store address of an instruction A variable used to store address of other variable A variable used to store address of a structure

A variable used to store address of other variable

What does malloc doe?

Allocates a block of memory but doesn't initialize it

What is the return type of this statement? malloc(10000); Choices: An address of generic data type void A pointer of type char* An array of 10000 bytes

An address of generic data type

struct node { int i; float j; }; struct node *s[10]; The above C declaration define 's' to be (GATE CS 2000) Choices: An array, each element of which is a pointer to a structure of type node A structure of 2 fields, each field being a pointer to an array of 10 elements A structure of 3 fields: an integer, a float, and an array of 10 elements An array, each element of which is a structure of type node.

An array, each element of which is a pointer to a structure of type node

Which of the following operators can be applied on structure variables? Choices: Equality comparison, == Assignment , = Both of the above None of the above

Assignment = (A structure variable can be assigned to other using =, but cannot be compared with other using ==)

Choose the application option for the following program? #include <stdio.h> main() { int *p, **q; printf("%u\n", sizeof(p)); printf("%u\n", sizeof(q)); } Choices: Both the printf() will print the same value First printf() prints the value less than the second. Second printf() prints the value less than the first. Error in the code.

Both the printf() will print the same value

Read following C program, and answer question. What does the function func do? int func (unsigned int x, unsigned int y) { return x & (01 << y); } Choices: Count the number of 0's in its binary representation of a given number. Check if the number of nonzero digits in its decimal representation equals to a given number Check if the y-th bit is 1 in the binary notation of a given number None of above

Check if the y-th bit is 1 in the binary notation of a given number

What is output? #include <stdio.h> main() { int const a = 5; a++; printf("%d", a); } Choices: 5 6 Runtime error Compile error

Compile error

What is the following program doing? #include <stdio.h> main() { FILE *stream=fopen("a.txt",'r'); } Choices: Trying to open "a.txt" in read mode Trying to open "a.txt" in write mode. "stream" is an invalid identifier Compile error

Compile error

What is the output? #include <stdio.h> main() { printf("\"); } Choices: \ \" " Compile error

Compile error

What is the output? #include<stdio.h> main() { for()printf("Hello"); } Choices: Infinity loop Prints "Hello" once No output Compile error

Compile error

What is the output? #include<stdio.h> main() { int r, x = 2; float y = 5; r = y%x; printf("%d", r); } Choices: 1 0 2 Compile error

Compile error

Output? int main() { { int var = 10; } { printf("%d", var); } return 0; } Choices: 10 Compiler Error Garbage Value

Compiler Error

What is output? #include<stdio.h> struct st { int x; struct st next; }; int main() { struct st temp; temp.x = 10; temp.next = temp; printf("%d", temp.next.x); return 0; } Choices: Compiler Error 10 Runtime Error Garbage Value

Compiler Error (A structure cannot contain a member of its own type because if this is allowed then it becomes impossible for compiler to know size of such struct. Although a pointer of same type can be a member because pointers of all types are of same size and compiler can calculate size of struct)

What is the size of 'int'? Choices: 2 4 8 Compiler dependent

Compiler dependent

What does the following code do? #include <stdio.h> int main() { int *p; p = (int *)malloc (sizeof(int)); *p=10; printf("%d\n",*p); free(p); return 0; }

Create an int pointer p, allocate memory, initialize *p to 10, writes the value and deallocate p

We use malloc and calloc for Choices: Dynamic memory allocation Static memory allocation Both dynamic and static memory allocation None of the above

Dynamic memory allocation (Both Calloc() and Malloc() are used for dynamic memory allocation)

In the below statement, ptr1 and ptr2 are uninitialized pointers to int i.e. they are pointing to some random address that may or may not be valid address.int* ptr1, ptr2; TRUE FALSE

FALSE

What is the output? int main() { char *ptr = "GeeksQuiz"; printf("%cn", *&*&*ptr); return 0; } Choices: Compiler Error Garbage Value Runtime Error G

G

What is output? # include <iostream> # include <string.h> using namespace std; struct Test { char str[20]; }; int main() { struct Test st1, st2; strcpy(st1.str, "GeeksQuiz"); st2 = st1; st1.str[0] = 'S'; cout << st2.str; return 0; } Choices: Segmentation Fault SeeksQuiz GeeksQuiz Compiler Error

GeeksQuiz (Array members are deeply copied when a struct variable is assigned to another one. See Are array members deeply copied? for more details.)

Read the following C program double sum(int x, int y) { return x + y; } int main() { int a = 1, b = 1; double c = sum( a, b); } For a, b, x , y , which one are parameters, and which one are arguments?

In double sum, int x and int y are parameters in the first line of the method. The argument would be x + y. In the main function the a and b in sum(a , b) are parameters. sum(a, b) is an argument

What is the problem with following code? #include<stdio.h> int main(){ int *p = (int *)malloc(sizeof(int)); p = NULL; free(p); } Choices: Compiler Error: free can't be applied on NULL pointer Memory Leak Dangling Pointer The program may crash as free() is called for NULL pointer.

Memory Leak (free() can be called for NULL pointer, so no problem with free function call. The problem is memory leak, p is allocated some memory which is not freed, but the pointer is assigned as NULL. The correct sequence should be following: free(p); p = NULL;)

Pick the best statement for the below program: #include "stdio.h" int main(){ union {int i1; int i2;} myVar = {.i2 =100}; printf("%d %d",myVar.i1, myVar.i2); return 0; } Choices: Compile error due to incorrect syntax of initialization No compile error and it'll print "0 100" No compile error and it'll print "100 100"

No compile error and it'll print "100 100" (Since fields/members of union share same memory, both i1 and i2 refer to same location. Also, since both i1 and i2 are of same type, initializing one would initialize the other as well implicitly)

What is output? #include<stdio.h> main() { char s[]="hello", t[]="hello"; if(s==t){ printf("eqaul strings"); } } Choices: Equal strings Unequal strings No output Compilation error

No output

What is the output of the following program? #include <stdio.h> void f() { printf("Hello\n"); } main(){ ; } Choices: No output Error, as the function is not called Error, as the function is defined without its declaration Error, as the main() function is left empty

No output

Read following C program, and answer question. What value will be returned if the function call is func(4,3)? int func (unsigned int x, unsigned int y) { return x & (01 << y); } Choices: None of the above 1 2 3

None of the above

Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed. union test { int x; char arr[4]; int y; }; int main() { union test t; t.x = 0; t.arr[1] = 'G'; printf("%s", t.arr); return 0; } Choices: Nothing is printed G Garbage character followed by 'G' Garbage character followed by 'G', followed by more garbage characters Compiler Error

Nothing is printed

Consider the following three C functions: [PI] int * g (void) { int x= 10; return (&x); } [P2] int * g (void) { int * px; *px= 10; return px; } [P3] int *g (void) { int *px; px = (int *) malloc (sizeof(int)); *px= 10; return px; } Which of the above three functions are likely to cause problems with pointers? (GATE 2001) Choices Only P3 Only P1 and P3 Only P1 and P2 P1, P2 and P3

Only P1 and P2 (In P1, pointer variable x is a local variable to g(), and g() returns pointer to this variable. x may vanish after g() has returned as x exists on stack. So, &x may become invalid. In P2, pointer variable px is being assigned a value without allocating memory to it. P3 works perfectly fine. Memory is allocated to pointer variable px using malloc(). So, px exists on heap, it's existence will remain in memory even after return of g() as it is on heap.)

Declare structure variables named c1, c2, and c3, each having members real and imaginary of type double. Write statements that copy the members of c2 into c1. Can this be done in one statement, or does it require two?

Only one statement is necessary, c1 = c2;

What is P? Typedef char * charp; const charp P; Choices: P is a constant P is a character type P is a pointer None of the above

P is a constant

What is the output? #include <stdio.h> main() { int *p = 15; printf("%d",*p); } Choices: 15 Garbage value Runtime error Compiler error

Runtime error

An exception is? Choices: Runtime error Compile time error Logical error None of the above

Runtime error (When program is in execution phase the possible unavoidable error is called as an exception)

What does this code do? void shuffle(int a[], int b[], int idxList[], int N){ int i = 0; for (i=0; i < N; i ++){ b[i] = a[idxList[i]]; } } int main(){ int a[5] = {1, 2, 3, 4, 5}; int idxList[5] = {3, 1, 2, 0, 4}; int b[5] = {0}; int N = 5; shuffle(a, b, idxList, N); } Show the content of the array b after each loop in the shuffle function

Shuffle function takes in three int arrays as Parameters and an int parameter as an index. Inside shuffle is a for-loop that reassigns N values of b using array a with the index of array idxList at i. b[0] = a[idxList[0]]; == a[3] == 4 b[1] = a[idxList[1]]; == a[1] == 2; b[2] = a[idxList[2]]; == a[2] == 3; b[3] = a[idxList[3]]; == a[3] == 1; b[4] = a[idxList[4]]; == a[4] == 5;

Choose the correct option in respect to the following program. #include <stdio.h> void f(int const i) { i=5; } main() { int x = 10; f(x); } Statements: Error in the statement 'void f(int const i)' Error in the statement i=5. Choices: Statements I & II are true Statements I & II are false. Statement I is true Statement II is true.

Statement II is true.

Anyone of the following can be used to declare a node for a singly linked list and "NODEPTR nodePtr;" can be used to declare pointer to a node using any of the following /* First declaration */ typedef struct node{ int data; struct node *nextPtr;}* NODEPTR; /* Second declaration */ struct node{ int data; struct node * nextPtr;}; typedef struct node * NODEPTR; Choices: TRUE FALSE

TRUE (Yes. Both are equivalent. Either of the above declarations can be used for "NODEPTR nodePtr;". In fact, first one is the compact form of second one.)

Assume the code below is written in C, what is wrong with it? int a = -4; boolean positive = true; if ( a < 0) positive = false else positive = true

The correct declaration is bool not boolean. To use bool you have to also import the bool header file. If you do not import the file it is better to make positive an int and set it to 0. ; are needed after line 4, positive = false, and line 6, positive = true. If you do not import the bool file make positive = 0, if (a < 0) and positive = 1 after the else statement.

Suppose that s is the following structure: struct { double a; union { char b[4]; double c; int d; } e; char f[4]; } s; If cahr is 1 byte, int is 4 byts, and double values 8 bytes, how much space will a C ompiler allocate for s? (Assume that the compiler leaves no "holes" between members.)

The member of the structure a takes 8 bytes, union e takes 8 bytes and the array f takes 4 bytes. Total bytes = 20 struct { double a; // 8 bytes union { //union takes 8 bytes char b[4]; //4 bytes double c; //8 bytes int d; //4 bytes } e; char f[4]; //4 bytes } s;

For the below definition what is the data type of 'PI' #define PI 3.141 Choices: Its float Its double There is no type associated with PI, as it's just a text substitution Syntax error, semi colon is missing with the definition of PI

There is no type associated with PI, as it's just a text substitution

Properties of realloc

When it expands a memory block, realloc doesn't initialize the bytes that are added to the block. Can't enlarge the memory block as requested, it returns a null pointer; the data in the old memory block is unchanged. If called with a null pointer as its first argument, it behaves like malloc. If called with 0 as its second argument, it frees the memory block.

Consider the following C program, which variable has the longest scope? int a; int main() { int b; // .. // .. } int c; Choices: a b c All have same scope

a

Choose the correct unary operators in C: a) ! b) ~ c) ^& d) ++

a,b,d

The following function calls supposedly write a single new-line character, but some are incorrect. Identify which calls don't work and explain why. a) printf("%c", '\n'); b) printf("%c", "\n"); c) printf("%s", '\n'); d) printf("%s", "\n"); e) printf('\n'); f) printf("\n"); g) putchar('\n'); h) putchar("\n"); i) pust('\n'); j) puts("\n"); k) puts("");

a- correct, b- Incorrect wrong format, c- Incorrect wrong format, d - correct, e - Incorrect printf statement doesn't have const character, f- correct, g- correct, h- Incorrect only for single character, i- Incorrect function puts will output the string pointed to by a string, j- correct, k - correct

Suppose that p has been declared as follows: char *p = "abc"; Which of the following function calls are legal? Show the output produced by each legal call, and explain why the others are illegal. a) putchar(p); b) putchar(*p); c) puts(p); d) puts(*p);

a- illegal function is used for only single character, b - legal, c- legal, d- illegal fuction is used for a string; p is a pointer

What is the output? #include<stdio.h> main() { int a = 5, b = 3, c = 4; printf("a = %d, b = %d\n", a, b, c); } Choices: a=5, b= 3 a=5, b=3, c=0 a=5, b=3, 0 compile error

a=5, b= 3

What does the "arr" indicate? char * arr[30]; Choices: arr is an array of function arr is an array of 30 characters arr is a pointer to an array arr is a array of 30 character pointers

arr is a array of 30 character pointers

Suppose that f and p are declared as follows: struct { union { char a, b; int c; } d; int e[5]; }f, *p = &f; Which of the following statements are legal? a) p->b = ' '; b) p->e[3] = 10; c) (*p).d.a = '*'; d) p->d->c = 20;

b, c (a is illegal because it initializes as pointer p which reference to the member variable in the union directly and also initializes to an empty character in single quotes. correct statement is p-> d.b = 'a'; d is illegal because of use of -> after d, should use '.' Correct: p->d.c = 20)

Declare structure variables named c1, c2, and c3, each having members real and imaginary of type double. Write statements that add the corresponding members of c1 and c2, storing the result in c3.

c3.real = c1.real + c2.real; c3.imaginary = c1.imaginary + c2.imaginary;

char strcat algorithm

char *strcat(char *s1, const char *s2) { char *p = s1; while (*p) p++; while (*p++ = *s2++); return s1; }

What will be the value of the string s1 after the following statements have been executed? strcpy(s1, "computer"); strcpy(s2, "science"); if (strcmp(s1,s2) < 0) strcat(s1,s2); else strcat(s2, s1); s1[strlen{s1)-6] = '\0';

computers

Suppose that str is an array of characters. Which one of the following statements is not equivalent to the other three? a) *str = 0; b) str[0] = '\0'; c) strcpy(str, ""); d) strcat(str, "");

d (strcat concatenates at the end of a string)

To store a word/sentence declare a variable of the type 'string'. Choices: true false

false (char [] = "string")

What is the value for i and j after statements executed? i = 1; j = 2; k = ++i + j++;

i = 2, j = 3, k = 4

Assuming you have to declare a constant to store the value 0xabcd. What is the most memory efficient data type to store it i.e. which data type uses the least memory of bytes in memory? Choices: int double long long long char float short

int

Suppose that i and j are variables of type int. What is the type of the expression?: i/j + 'a'; Choices: long char short int

int ('a' is a char, in C chars are small integers)

Writ the following function: int *create_array(int n, int initial_value); The function should return a pointer to a dynamically allocated int array with n members, each of which is initialized to initial_value. The return value should be NULL if array can't be allocated.

int *create_array(int n, in initial_value) { //declare pointer variables //dynamic memory allocation for pointer variable a int *p = malloc(n * sizeof(int)); if(p == NULL) { int x; for ( x = 0; x < n ; x++) //initialize the initial_value p[x] = initial_value; } return p; }

Read the following C program and answer the question int i = 0; int j = i ++; int k = ++ i; What is the value of j and k?

j = 0 and k = 2. J is assigned i with postfix. So i = 1 after j is assigned. K is assinged i prefix so i is incremented and then assigned to k.

What is the value of k? int i, j, k; i = 1; j = 2; k = i > j ? i : j;

k = 2

What is the value of k? int i, j, k; i = 1; j = 2; k = (i >= 0 ? i : 0) + j;

k = 3

The following declarations, the x and y structures have members named x and y. Is this legal on an individual basis; could both appear as shown in a program?: #include <stdio.h> int main(void) { struct { int x, y;} x; struct { s, y; } y; }

legal but both cannot be used at the same time

For the following item of data, specify which one of the types char, short, int, or long is the smallest one guaranteed to be large enough to store the item. Seconds in a day

long (range is 1-86400, long range is -2147483648-2147483647)

Given the code below. What would happen to pos.i and pos.j after the code above is executed: union POS{ short i; long j; } part1; union POS pos; pos.j = 0xabcdef12; pos.i = 0x00; Choices: no change to pos.i (0x00), while part of pos.j is overwritten pos.i is overwritten, pos.j is overwritten no change to pos.i (0x00), no change to pos.j (0xabcdef12) no change to pos.j (0xabcdef12), while part of pos.i is overwritten

no change to pos.i (0x00), while part of pos.j is overwritten

What does this following code do? void main() { int a[ ] = {1,2,3,4,5}, i = 0, N = 5; for(; i < N; i++) { int tmp = a[i]; a[i] = a [N - i]; a[N-i] = tmp; Choices: a run time error would occur since the index access is out of bound reverse an array nothing, the array remains the same reverse an array, except the middle item would stay the same there is a bug and the code won't compile

nothing, the array remains the same

What is the output? i = 1; switch (i % 3) { case 0: printf("zero"); case 1: printf("one"); case 2: printf("two"); }

onetwo ( no break, following cases are executed)

Assuming i is an integer, a is an array of integer from 1 to 8, and n has the value 8. What is the output of this code? for(i = 0; i <n; i++2) { print f("%3d", (i%2==0) ? a[i] : 0); Choices: print out all odd numbers in a print out all odd numbers in a, and insert number 0 between them print all number from 1 to 8 print out all even numbers in a print out zeros

print out zeros

If c is a variable of type char, which one of the following statements is illegal? Choices: i += c; /* i has type int */ c = 2 * c -1; putchar(c); printf(c);

printf(c); (should be printf("%c", c);)

What does the function do? unsigned int u; scanf("%u", &u); Choices: reads u in base 10 writes u What does the function do? unsigned int u; scanf("%u", &u); Choices: reads u in base 10 writes u in base 10 reads u in base 8 writes u in base 8 reads u in base 16 writes u in base 16

reads u in base 10

What does the function do? unsigned int u; scanf("%x", &u); Choices: reads u in base 10 writes u in base 10 reads u in base 8 writes u in base 8 reads u in base 16 writes u in base 16

reads u in base 16

What does the function do? unsigned int u; scanf("%o", &u); Choices: reads u in base 10 writes u in base 10 reads u in base 8 writes u in base 8 reads u in base 16 writes u in base 16

reads u in base 8

Predict the output of following program. Assume that the numbers are stored in 2's complement form. #include<stdio.h> int main() { unsigned int x = -1; int y = ~0; if (x == y) printf("same"); else printf("not same"); return 0; } Choices: same not same

same

Which scanf() statement will you use to scan a float value (a) and double value (b)? Float a; Double b; Choices: scanf("%f %f", &a, &b); scanf("%lf %lf", &a, &b); scanf("%Lf %Lf", &a, &b); scanf("%f %lf", &a, &b);

scanf("%f %lf", &a, &b);

For the following item of data, specify which one of the types char, short, int, or long is the smallest one guaranteed to be large enough to store the item. Minutes in a day

short (range is 1-1440, short range is -32768-32767 in 16 bit machine)

For the following item of data, specify which one of the types char, short, int, or long is the smallest one guaranteed to be large enough to store the item. Days in a year

short (range is 1-366, short range is -32768-32767 in 16 bit machine)

Which of the following are not legal types in C? Choices: short unsigned int short float long double unsigned long

short float (not standard floating type in C)

The type name/reserved word 'short' is ___ Choices: short long short char short float short int

short int

string length algorithm

size_t strlen(const char *s) { const char *p = s; while (*s) s++; return s- p; }

Consider a compiler where int takes 4 bytes, char takes 1 byte and pointer takes 4 bytes. #include <stdio.h> int main() { int arri[] = {1, 2 ,3}; int *ptri = arri; char arrc[] = {1, 2 ,3}; char *ptrc = arrc; printf("sizeof arri[] = %d ", sizeof(arri)); printf("sizeof ptri = %d ", sizeof(ptri)); printf("sizeof arrc[] = %d ", sizeof(arrc)); printf("sizeof ptrc = %d ", sizeof(ptrc)); return 0; } Choices: sizeof arri[] = 3, sizeof ptri = 4, sizeof arrc[] = 3, sizeof ptrc = 4 sizeof arri[] = 12, sizeof ptri = 4, sizeof arrc[] = 3, sizeof ptrc = 1 sizeof arri[] = 3, sizeof ptri = 4, sizeof arrc[] = 3, sizeof ptrc = 1 sizeof arri[] = 12, sizeof ptri = 4, sizeof arrc[] = 3, sizeof ptrc = 4

sizeof arri[] = 12, sizeof ptri = 4, sizeof arrc[] = 3, sizeof ptrc = 4

What is the output? #include<stdio.h> #include<string.h> #include<stdlib.h> /* Swaps strings by swapping data*/ void swap2(char *str1, char *str2) { char *temp = (char *)malloc((strlen(str1) + 1) * sizeof(char)); strcpy(temp, str1); strcpy(str1, str2); strcpy(str2, temp); free(temp); } int main() { char str1[10] = "geeks"; char str2[10] = "forgeeks"; swap2(str1, str2); printf("str1 is %s, str2 is %s", str1, str2); getchar(); return 0; }

str1 is forgeeks, str2 is geeks

What is the output? #include<stdio.h> /* Swaps strings by swapping pointers */ void swap1(char **str1_ptr, char **str2_ptr) { char *temp = *str1_ptr; *str1_ptr = *str2_ptr; *str2_ptr = temp; } int main() { char *str1 = "geeks"; char *str2 = "forgeeks"; swap1(&str1, &str2); printf("str1 is %s, str2 is %s", str1, str2); getchar(); return 0; }

str1 is forgeeks, str2 is geeks

What will be the value of the string str after the following statements have been executed? strcpy(str, "tire-bouchon"); strcpy(&str[4], "d-or-wi"); strcat(str, "red?");

tired-or-wired?

Prototype of calloc

void *calloc (size_t nmemb, size_t size);

The prototype for malloc function:

void *malloc(size_t size);

Prototype of realloc

void *realloc(void *ptr, size_t size); (ptr must point to a memory block obtained by a prvios call of malloc, calloc, or realloc; size represents the new size of the block, which may be larger or smaller than the original size.)

Correct the program below, which is used to compute the sum and average of the numbers in the array a. void avg_sum (float a[], int n, float *avg, float *sum) { int i; sum = 0.0; for (i = 0; i < n; i++) sum += a[i]; avg = sum /n; }

void avg_sum (float a[], int n, float *avg, float *sum) { int i; *sum = 0.0; for (i = 0; i < n; i++) *sum += a[i]; *avg = *sum /n; }

Which of the following function declaration is legal in C? Choices: compute(double x) double compute(x); function compute(double x); void compute(double x);

void compute(double x);

Write the following function void find_two_largest (int a[], int n, int *largest, int *second_largest); When passed an array a of length n, the function will search a for its largest and second-largest elements, storing them in the variables pointed to by largest and second_largest, respectively.

void find_two_larges(int a[], int n, int *largest, int *second_largest) { int i; //initializing variables based on condition if(a[0] > a[1]) { *largest = a[0]; *second_largest = a[1]; } else { *largest = a[1]; *second_largest =a[0]; } //loop to find the largest elements among the array of elements for(i =2; i < n; i++) if (a[i] > *largest) { *second_largest = *largest; *largest = a[i]; } else if (a[i] > *second_largest) *second_largest = a[i]; }

Write the following function: void swap (int *p, int *q); When passed the addresses of two variables, swap should exchange the values of the variables: swap(&i, &j); /* exchanges values of i and j */

void swap(int *p, int *q) { //variables used to allocate memory for data type int temp; temp = *p; *p = *q; *q = temp; }

What does the function do? unsigned int u; printf("%u", u); Choices: reads u in base 10 writes u in base 10 reads u in base 8 writes u in base 8 reads u in base 16 writes u in base 16

writes u in base 10

What does the function do? unsigned int u; printf("%x", u); Choices: reads u in base 10 writes u in base 10 reads u in base 8 writes u in base 8 reads u in base 16 writes u in base 16

writes u in base 16

Output of following program? #include <stdio.h> int main() { int *ptr; int x; ptr = &x; *ptr = 0; printf(" x = %dn", x); printf(" *ptr = %dn", *ptr); *ptr += 5; printf(" x = %dn", x); printf(" *ptr = %dn", *ptr); (*ptr)++; printf(" x = %dn", x); printf(" *ptr = %dn", *ptr); return 0; } Choices: x = 0, *ptr = 0 x = 5, *ptr = 5, x = 6, *ptr = 6 x = garbage value, *ptr = 0, x = garbage value, *ptr = 5, x = garbage value, *ptr = 6 x = 0, *ptr = 0, x = 5, *ptr = 5, x = garbage value, *ptr = garbage value x = 0, *ptr = 0, x = 0, *ptr = 0, x = 0, *ptr = 0

x = 0, *ptr = 0 x = 5, *ptr = 5, x = 6, *ptr = 6

What is the content of a after this code block is executed? int* a = calloc(3, 4); a[2] = 1; Choices: {N/A,N/A,1} {0,0,1,0,0,0,0,0,0} {0,0,1} {0,1,0}

{0,0,1}

What is the output? #include <stdio.h> int main(void) { int i; floatx; i = 40; x= 839.21f; printf("|%5.3d|",i): return 0; } Choices: |40| | 40| |40 | | 040|

| 040|

What is the Output? #include <stdio.h> int main(void) { int i; floatx; i = 40; x= 839.21f; printf("|%5d|",i): return 0; } Choices: |40| | 40| |40 | | 040|

| 40|

What is the output? #include <stdio.h> int main(void) { int i; floatx; i = 40; x= 839.21f; printf("|%10.3e|", x): return 0; } Choices: | 839.210| | 8.392e+02| |839.21 |

| 8.392e+02|

What is the output? #include <stdio.h> int main(void) { int i; floatx; i = 40; x= 839.21f; printf("|%d|",i): return 0; } Choices: |40| | 40| |40 | | 040|

|40|

What is the output? #include <stdio.h> int main(void) { int i; floatx; i = 40; x= 839.21f; printf("|%-10g", x): return 0; } Choices: | 839.210| | 8.392e+02| |839.21 |

|839.21 |

What is the output of the following program? #include <stdio.h> void swap(int m, int n( { int x = m; m = n; n = x; } main () { int x = 5, y = 3; swap(x,y); printf("%d %d", x, y); } Choices: 3 5 5 3 5 5 Compile error

5 3

Which of the following is true Choices: gets() can read a string with newline chacters but a normal scanf() with %s can not. gets() can read a string with spaces but a normal scanf() with %s can not. gets() can always replace scanf() without any additional code. None of the above

gets() can read a string with spaces but a normal scanf() with %s can not.

Which of the following is true Choices: gets() doesn't do any array bound testing and should not be used. fgets() should be used in place of gets() only for files, otherwise gets() is fine gets() cannot read strings with spaces None of the above

gets() doesn't do any array bound testing and should not be used.

Predict the output of following C program #include <stdio.h> int main() { char a = 012; printf("%d", a); return 0; } Choices: Compiler Error 12 10 Empty

10

What is the value of variable y in this program? #include <stdio.h> void main(){ unsigned short x = 0xabcd; unsigned short y = (x & 0x04) << 7; } Choices: 128 1024 11 256 512 7

512

What is the minimum number of bytes in memory should be allocated to the struct below: struct POS { short i; long j; } part1; Choices: 2 4 6 5

6

What is the output? #include <stdio.h> main() { union abc { int x; char ch; } var; var.ch = 'A'; printf("%d", var.x); Choices: A Garbage value 65 97

65 (union variables share common memory for all its' elements, 'A' ascii value is 65)

In a C program snippet, followings are used for definition of Integer variables? signed s; unsigned u; long l; long long ll; Pick the best statement for these: All of the above variable definitions are incorrect because basic data type int is missing. All of the above variable definitions are correct because int is implicitly assumed in all of these. Only "long l;" and "long long ll;" are valid definitions of variables. Only "unsigned u;" is valid definition of variable.

All of the above variable definitions are correct because int is implicitly assumed in all of these.

Output? int main(){ void *vptr, v; v = 0; vptr = &v; printf("%v", *vptr); getchar(); return 0; } 0 Compiler Error Garbage Value

Compiler Error (void is not a valid type for declaring variables. void * is valid though.)

What is the output of the following program? #include <stdio.h> void main() { float x = 3.789; printf("Hello World!"); printf("%3.4f", x); } Choices: Hello World!!3.789 Hello World!!3.7890 Hello World!!3.78 Hello World!!3.4

Hello World!!3.7890

There is a very simple C program: #include <stdio.h> int main(){ printf("Hello world"); printf("This is CSC3320); return 0; } What is the expected output? Choices: Hello worldThis is CSC3320 No output, since there is an error in the program Hello, world This is CSC3320 "Hello world" "This is CSC3320"

Hello worldThis is CSC3320

Predict the output #include <stdio.h> int main() { float c = 5.0; printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32); return 0; } Choices: Temperature in Fahrenheit is 41.00 Temperature in Fahrenheit is 37.00 Temperature in Fahrenheit is 0.00 Compiler Error

Temperature in Fahrenheit is 37.00

#include <stdio.h> int main() { printf(" \"GEEKS %% FOR %% GEEKS\" "); getchar(); return 0; } Choices: "GEEKS % FOR % GEEKS" GEEKS % FOR % GEEKS \"GEEKS %% FOR %% GEEKS\" GEEKS %% FOR %% GEEKS

"GEEKS % FOR % GEEKS"

In C language, which of the followings is the directive? Choices: printf("Hello"); scanf(&v); return 0; #include <stdio.h>

#include <stdio.h>

Assume that the size of char is 1 byte and negatives are stored in 2's complement form #include <stdio.h> int main(){ char c = 125; c = c+10; printf("%d", c); return 0; } 135 +INF -121 -8

-121

Given this code below: int a[4] = {1,2,3,4}; And the following address table: Variable Address a[0] 0xe0 a[2] 0xe8 a[3] 0xec What is the address of a[4], then calculate the result of this expression: &a[0] - &a[4] Choices: 3 -16 0x10 -4

-4

For a structure, if available behave as a pointer then from the given below operators which operator can be used to access data of the structure via the variable pointer? Choices: . % -> #

->

Given the code below int x = 3, *p, *q, *r; p = &x; q = p; *&r = *&q; x = *r + *q + 1; What is the value of x? Choices: 7 Nothing, since there is a compilation error address of r 4 3 &q + 1, which is the address of q plus 1

7

The syntax for which of the following commands are incorrect? Choices: printf("Hello\n$"); int x = ++3; #define circleArea(r) (3.1415*(r)*(r)) #include <stdio.h>;

#include <stdio.h>;

Given the following code: int * p, q = 3; p = &q; *p = *p +1; What is the value of p after the code above is executed? 4 &q + 1, which is the address of q plus 1 &q, which is the address of q 3 Undefined

&q, which is the address of q

What happened when this code is executed? #include "stdlib.h" #include "stdio.h" #include "unistd.h" void main() { int i = 11; char* p = malloc(1024*1024*1024*1); sleep(20); } Choices: 1 Gb is expected to be allocated to the program if it actual use it in future. 1 Gb of memory is allocated to the program.

1 Gb is expected to be allocated to the program if it actual use it in future.

What is the output? #include <stdio.h> main() { unsigned x = 5, y= &x, *p = y +0; printf("%u", p); } Choices: Address of x Address of y Address of p 5

5 (P holds the address of x which is y+0)

Given the code below int x = 3, *p = &x, *q; x = *&x + *p +x; What is the value of x? Some address in the memory since q is a sum of addresses and integer values Nothing, since there is a compilation error 3 9

9

What is wrong with the code below? struct COORD { int x; int y; }; char* p = malloc(sizeof(struct COORD)); char* q = malloc(sizeof(struct COORD)); p->x = 123; p = q; p->x = 123; Choices: A potential memory leak may happen There maybe not enough memory allocated to q There is a compilation error There is nothing wrong

A potential memory leak may happen

What is the expected output of the following code? struct COORD{ int x; int y; }; char* p = malloc(8); struct COORD* q = p; q->x = 65 + (67 << 8); q->y = 66; printf("%s\n", p); Choices: 6567 6567066 AC Run time error

AC

Similarity between a structure union and enumeration, Choices: All are helpful in defining new variables All are helpful in defining new data types All are helpful in defining new pointers All are helpful in defining new structures

All are helpful in defining new data types (A structure, union and enumeration all of them can define a new data type)

Consider the program below in a hypothetical language which allows global variable and a choice of call by reference or call by value methods of parameter passing. int i ; program main () { int j = 60; i = 50; call f (i, j); print i, j; } procedure f (x, y) { i = 100; x = 10; y = y + i ; } Choices: Call by value : i = 70, j = 10; Call by reference : i = 60, j = 70 Call by value : i = 50, j = 60; Call by reference : i = 50, j = 70 Call by value : i = 10, j = 70; Call by reference : i = 100, j = 60 Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70

Call by value : i = 100, j = 60; Call by reference : i = 10, j = 70

Given the following code int *p What is the data type of p Choices: int * unsigned int Pointer, which is the address of the memory area p is pointing to int

Pointer, which is the address of the memory area p is pointing to

Given the code below. What is the proper way to resize c, so that it can hold 5 individual struct COORD? sturct COORD{ int x; int y; }; struct COORD *c; c = (struct COORD*) calloc(3, sizeof(COORD)); Choices: c = (struct COORD*) malloc(sizeof(COORD)*%5; c = (struct COORD*) calloc(3, sizeof(COORD)); c = realloc(sizeof(COORD)*5); c = (struct(COORD*) realloc(c, sizeof(COORD)*5);

c = (struct(COORD*) realloc(c, sizeof(COORD)*5);

Output? #include <stdio.h> int main() { int x = 1, y = 2, z = 3; printf(" x = %d, y = %d, z = %d n", x, y, z); { int x = 10; float y = 20; printf(" x = %d, y = %f, z = %d n", x, y, z); { int z = 100; printf(" x = %d, y = %f, z = %d n", x, y, z); } } return 0; } Choices: x = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 1, y = 2, z = 100 Compiler Error x = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 10, y = 20.000000, z = 100 x = 1, y = 2, z = 3 x = 1, y = 2, z = 3 x = 1, y = 2, z = 3

x = 1, y = 2, z = 3 x = 10, y = 20.000000, z = 3 x = 10, y = 20.000000, z = 100

#include <stdio.h> extern int var; int main() { var = 10; printf("%d ", var); return 0; } Choices: Compiler Error: var is not defined 20 0

Compiler Error: var is not defined

What are the values of these statement? strcmp("apple", "Apple"); strcmp("constant", "constantinople"); strcmp("hope", "hope"); Choices: -1, 1, and 0 0, 0, and 1 1, -1, and 0 1, 0, and 0 1, 1, and 0

1, -1, and 0

Given the code below, What is the proper way to allocate memory for variable c?: sturct COORD{ int x; int y; }; struct COORD *c; Choices: (struct COORD*) calloc(1, sizeof(COORD)); (struct COORD*) malloc(4); calloc(1, sizeof(COORD)); (struct COORD*) calloc(8, sizeof(COORD));

(struct COORD*) calloc(1, sizeof(COORD));

Output of following program? #include<stdio.h> int main() { float x = 0.1; if ( x == 0.1 ) printf("IF"); else if (x == 0.1f) printf("ELSE IF"); else printf("ELSE"); } ELSE IF IF ELSE

ELSE IF

What is the output? #include <stdio.h> main () { f(;;)printf("Hello"); } Choices: Infinite loop Prints "Hello" once No output Compile error

Infinite loop

Predict the output of below program: #include <stdio.h> int main() { printf("%c ", "GeeksQuiz"[5]); return 0; } Choices: Compile-time error Runtime error Q s

Q

What will be the contents of the array a after the following statements are executed? int a[N] = {1,2,3,4,5,6,7,8,9,10}; int *p = &a[0], *q=&a[N-1], temp; while (p<q) { temp = *p; *p++ = *q; *q-- = temp; } Choices: {1,2,3,4,5,6,7,8,9,10} {10,9,8,7,6,5,4,3,2,1} {1,9,8,7,6,5,4,3,2,10} {10,2,3,4,5,6,7,8,9,1}

{10,9,8,7,6,5,4,3,2,1}

What is the value of the variable x in the code below? int f (int n) { if ( n < -16) retrun 1; else return n * f(n-1); } void main() { int x = f(3); } Choices: Nothing since there is a run time error -16 Nothing there is a compilation error 0 -1 2.092279e+13

0

What will be the output of the code below? #include <stdio.h> void main() { float x = 3.1412; int z = x - (int)x; printf("%d", z); } Choices: Nothing since there is a warning in the program 0.1412 3.1412 0.141200 0 Nothing since then is an error in the program

0

What will be the value of the variable q before the program terminate? #include <stdio.h> void main() { float x = 3.1.412; float q = x - (int)x; } Choices: 3.14 0.141200 Nothing, since there is a warning in the program 3.1412 3 Nothing since there is an error in the program

0.141200

The following is the content of the file hello.c #include <stdio.h> void main() { int x = 0x123456; short y = x; } What will be the value of the variable y? 0x56 0x1234 0x123456 0x12 0x3456 Nothing, since there is an error in the program

0x3456

What is the value of the variable y in this program? #include <stdio.h> void main(){ unsigned short x = 0xabcd; unsigned short y = x << 2; } Choices: 0x1af34 0x34 0x2af34 0xaf34 0xf34

0xaf34

Given this code below: int a[4] = {1,2,3,4}; And the following address table: Variable Address a[0] 0xe0 a[2] 0xe8 a[3] 0xec What is the address of a[1] 0xe6 0xe4 0xe0 0xe2

0xe4

Given this code below: int a[4] = {1,2,3,4}; And the following address table: Variable Address a[0] 0xe0 a[2] 0xe8 a[3] 0xec What is the value of following statement: &a[2] < &a[3] Choices: True False 0 Nothing, the statement is invalid 1

1

Predict the output of following program? #include "stdio.h" int main() { char arr[100]; printf("%d", scanf("%s", arr)); /* Suppose that input value given for above scanf is "GeeksQuiz" */ return 1; } 9 1 10 100

1 (In C, scanf returns the no. of inputs it has successfully read.)

What is the output? #include <stdio.h> void f() { static int i; ++i; printf("%d", i); } main() { f(); f(); f(); } Choices: 1 1 1 0 0 0 3 2 1 1 2 3

1 2 3 (static local variables retains its value between the function calls and the default value is 0)

#include <stdio.h> int main() { printf("Hello world"); return 110; } After compiling into executable file named a.out. Execute a.out then right after that invoke this command: echo $? What is the expected output for echo $? 0 -1 110 Nothing will show up

110

What is the correct input for the following program? #include <stdio.h> void main() { char a1,a2,a3,a4,a5,a6,a7,a8,a9,a10; printf("Input phone ###-####-###:"); scanf("%c%c%c-%c%c%c%c- %c%c%c", &a1,&a2,&a3,&a4,&a5,&a6,&a7,&a8,&a9,&a10); printf("%c%c%c-%c%c%c%c- %c%c%c", a1,a2,a3,a4,a5,a6,a7,a8,a9,a10); Choices: 123-456-78910 123-456-7890 123-hell-OoO 123-456-789

123-hell-OoO

Given the code below, what is the value of s->key: struct ST{ int key; struct ST* next; }; struct ST* r = (struct ST*) malloc(sizeof(struct ST)); struct ST* s = (struct ST*) malloc(sizeof(struct ST)); r->next = s; r->key = 1; r->next->key = 2; Choices: address of r->next 1 N/A 2

2

What is the output of the following program? #include int tmp=20; main( ) { printf("%d ",tmp); func( ); printf("%d ",tmp); } func( ) { static int tmp=10; printf("%d ",tmp); } Choices: 20 10 10 20 10 20 20 20 20 10 10 10

20 10 20

Given this code below: int a[4] = {1,2,3,4}, *p = &a[0], *q = &a[3]; And the following address table: Variable Address a[0] 0xe0 a[2] 0xe8 a[3] 0xec What is the value of the following expression: q-p Chocies; 2 0x0c 3 12

3

How many times has the function f been called in total? int f(n) { return n<=2 ? 2:f(n-1) * f(n-2); } void main() { printf("%d\n", f(3)); } Choices: 4 Once, since there is a runtime error 1 3 None, since there is a compilation error 2

3

What is the correct input for the following program? #include <stdio.h> void main() { int x, y; printf("input functional number a/b:"); scanf("%d/%d); } Choices: 20/-30.4 20.2/30 3 / 5 3/5

3/5

How many errors exist in the following code? #include <stdio.h>; void main(); { int x3 = 0; scanf("%d", x3); int x = x3 +1; int RESULT; result = x * x; return RESULT; Choices: 1 6 2 4 5 0

5

What is the output? #include <stdio.h> main(){ char s[20] = "Hello\0Hi"; printf("%d %d", strlen(s), sizeof(s)); } Choices: 5 9 7 20 5 20 8 20

5 20 (Length of string is count of character up to '\0'. sizeof - reports the size of the array)

Given the code below: char a[20] = {'h', 'e', 'l', 'l', 'o', '\0', 'w', 'o'. 'r', 'l', 'd', '\0'}; What is the value of strlen(a); and size of (a); respectively? Choices: 5 and 5 20 and 50 5 and 20 20 and 5

5 and 20

"typedef" in C basically works as an alias. Which of the following is correct for "typedef"? typedef can be used to alias compound data types such as struct and union. typedef can be used to alias both compound data types and pointer to these compound types. typedef can be used to alias a function pointer. typedef can be used to alias an array. All of the above

All of the above (In C, typedef can be used to alias or make synonyms of other types. Since function pointer is also a type, typedef can be used here. Since, array is also a type of symmetric data types, typedef can be used to alias array as well)

What is wrong with the code below? #include <stdio.h> void main() { int a[4] , i; for (i = 1; i <= 8; i++) { a[i] = 0; } } Choices: It loops forever because of a bug in the compiler An run time error occurs because value of index is greater than 4 There is a bug in the code. It won't compile It loops forever because the variable i is reset to value 0

It loops forever because the variable i is reset to value 0

What is wrong with the function f below? int f (int a[]) { int result = 0, i = 0; for (; i < sizeof(a)/sizeof(int); i ++) result += a[i]; return result; } void mian() { int a [ ] = {1, 2, 3}; int x = f(a); } Choices: Zero since there is a runtime error Nothing, there is a compilation error Nothing wrong it return the sum of the array a It underestimate the actual length of the array a It overestimate the actual length of the array a

It underestimate the actual length of the array a

Predict the output of the below program: #include <stdio.h> int main(){ printf("%c ", 5["GeeksQuiz"]); return 0; } Choices: Compile-time error Runtime error Q s

Q

What is the function used to ask for memory blocks from heap? And what is the name of the header files needed to be declared? calloc, and there's no need to declare any library calloc and stdio.h malloc and stdlib.h new and stdlib.h

malloc and stdlib.h

In a C program, following variables are defined: float x = 2.17; double y = 2.17; long double z = 2.17; Which of the following is correct way for printing these variables via printf. printf("%f %lf %Lf",x,y,z); printf("%f %f %f",x,y,z); printf("%f %ff %fff",x,y,z); printf("%f %lf %llf",x,y,z);

printf("%f %lf %Lf",x,y,z); (In C language, float, double and long double are called real data types. For "float", "double" and "long double", the right format specifiers are %f, %lf and %Lf from the above options. It should be noted that C standard has specified other format specifiers as well for real types which are %g, %e etc.)

Which of the following is not a valid declaration in C? 1. short int x; 2. signed short x; 3. short x; 4. unsigned short x; Choices: 3 and 4 2 1 All are valid

All are valid

Suppose a C program has floating constant 1.414, what's the best way to convert this as "float" data type? (float)1.414 float(1.414) 1.414f or 1.414F 1.414 itself of "float" data type i.e. nothing else required.

1.414f or 1.414F (By default floating constant is of double data type. By suffixing it with f or F, it can be converted to float data type. For more details, this post can be referred here)

#include <stdio.h> extern int var = 0; int main() { var = 10; printf("%d ", var); return 0; } Choices: 10 Compiler Error: var is not defined 0

10

#include <stdio.h> int main() { int i; for ( i=0; i<5; i++ ) { int i = 10; printf ( "%d ", i ); i++; } return 0; } Choices 10 11 12 13 14 10 10 10 10 10 0 1 2 3 4 Compilation error

10 10 10 10 10

Given the code below char str1[6] = "abc"; strcat (str1, "def"); What is the content of str1? {'a', 'b', 'c', 'd', 'e', 'f'} {'a', 'b', 'c', '\0', 'd', 'e', 'f', '\0'} {'a', 'b', 'c', 'd', 'e', 'f', '\0'} {'a', 'b', 'c', 'd', 'e', '\0'}

{'a', 'b', 'c', 'd', 'e', 'f', '\0'}

What is the content of the array a after this code is executed? void main () { int i = 0; int a[3] = {1,2,3} , N=3; while (i < N) a[++i] = 0; } Choices: All zeros, since a runtime error would occur Nothing, there would be a compilation error {0,0,0} {1,0,0} {0,2,3} {1,2,3}

{1,0,0}

What is the content of the array, a, after this code is executed? int a[3][3] = {1, 0}; a[1][2] = 1; Choices: {1,0,0},{0,0,0},{1,0,0} Nothing, there would be a compilation error All zeros, since there would be a run time error {1,0,0},{0,0,1},{0,0,0} {1,0,1},{0,0,0},{0,0,0}

{1,0,0},{0,0,1},{0,0,0}

In C, sizes of an integer and a pointer must be same. Choices: True False

False

What does the following code do? FILE *fp; fp = fopen("test.txt", "w"); fputs("test.txt", fp); fclose(fp); Choices: Open the file named "test.txt", then put all content in the file into the buffer variable fp; Write fp into the file named "test.txt" Write "test.txt" into the file named "test.txt" Open the file named "test.txt" then close it

Write "test.txt" into the file named "test.txt"

Predict output of the following program #include <stdio.h> int main(){ printf("new_c_questionby"); printf("rgeeksforgeeks"); getchar(); return 0; } Choices: ew_c_questiongeeksforgeeks new_c_quesgeeksforgeeks geeksforgeeks Depends on terminal configuration

Depends on terminal configuration

Find out the correct statement for the following program. #include "stdio.h" int * gPtr; int main() { int * lPtr = NULL; if(gPtr == lPtr) { printf("Equal!"); } else { printf("Not Equal"); } return 0; } Choices: It'll always print Equal. It'll always print Not Equal. Since gPtr isn't initialized in the program, it'll print sometimes Equal and at other times Not Equal.

It'll always print Equal.

#include <stdio.h> int main() { if (sizeof(int) > -1) printf("Yes"); else printf("No"); return 0; } Yes No Compiler Error Runtime Error

No

What is the value of variable z? void main() { double x =1, y =3; double z = average(x,y); } double average( double a, double b){ return (a + b)/2; } Zeros, since there is a run time error Nothing, there is a compilation error in the program 3 2 4 1

Nothing, there is a compilation error in the program

#include <stdio.h> // Assume base address of "GeeksQuiz" to be 1000 int main() { printf(5 + "GeeksQuiz"); return 0; } Choices: GeeksQuiz Quiz 1005 Compile-time error

Quiz (printf is a library function defined under stdio.h header file. The compiler adds 5 to the base address of the string through the expression 5 + "GeeksQuiz" . Then the string "Quiz" gets passed to the standard library function as an argument.)

Whcihc files will get closed through the fclose() in the following program? #include <stdio.h> int main() { FILE *fs, *ft, *fp; fp = fopen("ABC", "r"); fs = fopen("ACD", "r"); ft = fopen("ADF" , "r"); fclose(fp, fs, ft); return 0; } Choices: "ABC" "ACD" "ADF" Return error

Return error (syntax of fclose() function is wrong; it should be int fclose(FILE *stream); closes the stream)

When an array is passed as parameter to a function, which of the following statements is correct? Choices: The function can change values in the original array. In C, parameters are passed by value, the function cannot change the original value in the array. It results in compilation error when the function tries to access the elements in the array. Results in a run time error when the function tries to access the elements in the array.

The function can change values in the original array.


Ensembles d'études connexes

Chapter 9: Flexible Budgets, Standard Costs, and Variance Analysis

View Set