This quiz has 30 questions. Each question has four options (A, B, C, D). Choose the one most suitable option. The suggested time for the quiz is 30 minutes.
char greeting[] = "Hello World!";A. e
B. ll
C. o
D. Hell
unsigned int a = 100;
int b = -20;
if (b < a) printf("I expected this.\n");
else printf("I didn't expected this!\n");A. Output is I expected this.
B. Output is I didn't expected this!
C. Build error due to syntax issues
D. Program will run but will crash
int x = 5;
int y = x++;
printf("%d, %d", x, y);A. 5, 5
B. 5, 6
C. 6, 5
D. 6, 6
int* a, b;A. Both a and b are pointers to int
B. a is a pointer to int and b is an int
C. Both a and b are int
D. None of the above
char greeting[] = "Hello\0World!";A. 12
B. 11
C. 13
D. 5
A. [-2^32, 2^32]
B. [0, 2^32-1]
C. [-2^31, 2^31-1]
D. [-2^31+1, 2^31-1]
A. fopen, fclose
B. malloc, free
C. printf, scanf
D. atoi, memset
int num[5] = {10, 20, 30};
int *ptr = num;
*(ptr+1) += 5;A. {15, 20, 30}
B. {10, 25, 30}
C. {10, 25, 30, 0, 0}
D. {10, 20, 30, 5, 0}
float value = 5 / 2;
printf("%.1f", value);A. 2.5
B. 2.50
C. 2.0
D. .5
int a = 0x1122DE31;
int b = a & 0x1100;
printf("%x, %x", a>>8, b);A. 1122de, 1000
B. 0, 1100
C. 1122DE31, 1000
D. 1122de31, 1100
int x = 0;
int y = 10;
if ((x & 0x10) && ++y) {}
printf("%d, %d", x, y);A. 0, 11
B. 0, 10
C. 2, 11
D. 2, 10
#include <stdio.h>
int x = 3;
void update(int a) {
int x = 1;
static int y = 5;
x += a;
y += a;
printf("x:%d, y:%d; ", x, y);
}
int main() {
update(x);
x += 4;
printf("x:%d; ", x);
update(2);
return 0;
}A. x:8, y:8; x:12; x:14, y:10;
B. x:4, y:8; x:7; x:3, y:10;
C. x:8, y:8; x:7; x:3, y:7;
D. x:4, y:8; x:12; x:14, y:7;
char *str = "Hello";
str[0] = 'Y';
printf("%s", str);A. Yello
B. Hello
C. Build error
D. Runtime error
char greeting[] = "Hello";
char *ptr = "Hello";
printf("%zu, %zu", sizeof(greeting), sizeof(ptr));A. 5, 5
B. 6, 8
C. 6, 4
D. 5, 8
char src[] = "Data";
char dst[3];
strcpy(dst, src);A. dst has the value Data
B. dst has the value Dat but there's also undefined or unsafe behaviour
C. dst has the value Dat
D. dst has the value Da plus a NUL-terminating character
double *ptr = 0x1000;
printf("%p", ptr + 1);A. 0x1001
B. 0x1004
C. 0x1008
D. 0x1000
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += (i % 3) ? 0 : i;
}
printf("%d", sum);A. 27
B. 24
C. 20
D. 18
int matrix[2][2] = {
{1, 3},
{5, 7}
};A. **(matrix+1)+1
B. matrix[0][1]
C. **(matrix+1)
D. *(*matrix+1)
A. 13
B. 5
C. 3
D. 7
int x = 0;
if (x > 1)
if (x < 10)
printf("In range");
else
printf("Out of range");A. In range
B. Out of range
C. Build error
D. Program will run but no output
char name[] = "Krishna";
const char *p = name;
char const *q = name;
char * const r = name;A. p++;
B. q++;
C. r++;
D. *(r+1) = 'x';
char *str = "456";
printf("%s %c %d %d", str, str[0], str[0], atoi(str)+1);A. 4 4 4 5
B. 456 4 52 5
C. 456 4 52 457
D. 456 4 25 457
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
int w = *ptr++;
int x = (*ptr)++;
int y = ++*ptr;
int z = *++ptr;
printf("%d, %d, %d, %d", w, x, y, z);A. 11 21 31 41
B. 10 20 31 41
C. 10 22 30 40
D. 10 20 22 30
#include <stdio.h>
#define SQUARE(x) (x * x)
void main()
{
int a = 3, b = 2;
printf("%d", SQUARE(a+b));
}A. 25
B. 13
C. 11
D. 16
A.
size_t mystrlen( const char* str ) {
size_t len;
for (len = 0; str[len] != '\0'; ++len);
return len;
}B.
size_t mystrlen( const char* str ) {
size_t len = 0;
while (*str++) len++;
return len;
}C.
size_t mystrlen( const char* str ) {
char *p = str;
while (*p++);
return p - str - 1;
}D. All of the above
26. Given that reg is an unsigned int, which code flips bit 3 (4th bit) and keeps the other bits unchanged?
A.
reg |= 1 << 4;B.
reg &= 1 << 4;C.
reg ^= 1 << 3;D.
reg |= 0xF7 >> 3;char buffer[] = "Good versus Bad";
memset(buffer, 'X', 4);
strncpy(buffer + 3, "Not", 2);
printf("%s", buffer);A. XXXNoversus Bad
B. Good versus Bad
C. XXXXNotversusBa
D. XXXX Not versus
A.
int c = 0;
while (*str)
if (strchr("aeiou", tolower(*str))) c++;B.
int c = 0;
while (*str++)
if (strchr("aeiou", tolower(*str))) c++;C.
int c = 0;
while (*str)
if (strchr("AEIOUaeiou", *str)) c++;D.
int c = 0;
while (*str)
if (strchr("AEIOUaeiou", *str++)) c++;char s1[] = "abc";
char *s2 = "abc";
if (s1 == s2) printf("Strings are equal");
else printf("Strings are not equal");A. Strings are equal
B. Strings are not equal
C. Build error
D. None of the above
typedef struct {
char first_name[15];
char last_name[15];
unsigned int age;
} Student;
int main() {
Student cse_class[32] = {
{ "Akash", "Kumar", 18 },
{ "Krishna", "S", 17},
{ "Ramya", "Verma" }
};
}A. Compilation error because Ramya's age is not initialised
B. 34 bytes are allocated for each student
C. Student data is initialised only for three students
D. 36 bytes are allocated for each student
- [C] Arrays start from zero index.
- [B] Since
intis compared tounsigned int, it is implicitly converted tounsigned int. Thus, -20 becomes a big positive number. For implicit conversion rules, see https://www.scaler.com/topics/c/implicit-type-conversion-in-c/ - [C] Value
xis assigned toyand then it is incremented in-place. - [B] The
*is associated with the variable name and not the type. It's better to write this asint *a, b;if single-line declaration is preferred. - [D] The string is terminated by
'\0'. Hence it's only 5 characters though more memory is allocated togreeting. - [C] See https://os.mbed.com/handbook/C-Data-Types
- [D] In options A, B and C, one function does the opposite of the other: open/close file, allocate/free memory and output/input message. Functions
atoiandmemsetare not related in this way. - [C] The last two items of the array are default initialized to zero.
- [C] The division is performed on integers and therefore the result gets truncated to an integer even though it's subsequently assigned to a
floattype. Correct division can be done with(float)5 / 2or simply5. / 2to force decimal division. - [A] The two least significant bytes of
ais 0xDE31 (1101 1110 0011 0001in binary). If we do bitwise AND with 0x1100 (0001 0001 0000 0000in binary) we get0001 0000 0000 0000, which is 0x1000 in hexadecimal. Bit shifting by 8 bits to the right means that leftmost 8 bits are lost and more significant bits are shifted lower. Formatting%xprints in lowercase. - [B] Left-side expression of
&&evaluates to zero, which implies false. With AND, there's no point in evaluating the right-side expression. Hence,yis not incremented. This is called lazy evaluation. Lazy evaluation can happen for||as well if the left-side expression evaluates to true. - [B] This tests understanding of variable scope. Top
xis global variable.xandyinsideupdateare local to that function but sinceyis static it's initialized only once and the variable persists across multiple calls to the function. Withx += 4;inmain, the global variable is updated. - [D] "Hello" is a string literal. It's allocated in a read-only memory section. Hence it can't be modified. However, a smart modern compiler when used with the correct compilation flags can catch this problem at build time. The way to initialise a string on the stack is by doing
char str[] = "Hello";instead. - [B] Note that
sizeofcounts the NUL character at the end unlikestrlen. Most modern systems are 64-bit systems. Hence the pointer takes up 8 bytes. Older 32-bit system allocate 4 bytes for a pointer. Forprintfformatting options, see https://en.cppreference.com/c/io/fprintf - [B] Array
dstis smaller thansrc. Functionstrcpycopies the data beyond the allowable boundary ofdst. See https://en.cppreference.com/c/string/byte/strcpy - [C] The type
doubletakes up 8 bytes. When we doptr + 1, the pointer moves to the next double, hence 8 bytes further. - [D] Integers 0-9 are processed.
(i % 3)is zero for integers divisible by 3, that is, [3, 6, 9]. In these cases, they are added tosum, else only0is added. - [C] The expressions
**(matrix+1),matrix[1][0]and*matrix[1]are equivalent. - [A] This relates to operator precedence. Multiplication happens before addition. Bit shifting happens next. Finally, bitwise XOR happens. The expression is equivalent to
(3 + 2) ^ ( (6 + (9*3))>>2 )=5 ^ ((6+27)>>2)=5 ^ (33>>2)=5 ^ (00100001b>>2)=0101b ^ 1000b=1101b=13. See https://en.cppreference.com/c/language/operator_precedence - [D] Code indentation in C language is for code readability. Compiler doesn't care about code indentation. It associates the
elsewith the secondif. This sort of problem can be avoided by always using{}withif-elsestatement. - [C]
pandqare equivalent. They are pointers to a constant string. Thoughnameis not constant and can be changed directly, it can't be changed viaporq. Pointerspandqcan be changed to point to another location.ris a constant pointer to a variable string. String itself can be changed viarbutrcan't be changed to point to another location. The keywordconstmust be associated with what precedes it. Going by this rule, the syntax ofqis preferred over that ofp. - [C] Character
4has ASCII value 52. It's worth remembering the following ASCII values:0:0x30,A:0x41,a:0x61. See https://www.ascii-code.com/ - [D] For
w, value is read, assigned towand postfix increment on the pointer is done. In fact, even if we were to writew = *(ptr++);postfix increment happens last afterwis assigned the value10. Forx, value is read, assigned toxand postfix increment on the value is done (20 becomes 21). Fory, value is accessed, incremented in-place (21 becomes 22) and then assigned toy. Forz, pointer is prefix incremented, value is read and assigned toz. Note that pointer is increment only inwandzstatements. Inxandystatements, value is incremented. - [C] Preprocessor replaces
SQUARE(a+b)with(a+b * a+b), which is effectivelya + b*a + b. The proper way to write the macro is#define SQUARE(x) ((x) * (x)). Alternatively, call with parentheses,SQUARE((a+b)), which will become(a + b)*(a + b). - [D] In option C, pointer moves beyond
'\0'character because of postfix increment. Hence-1is used to account for this. Because of pointer arithmetic, option C doesn't a separate variablelento keep track of the count. - [C]
1 << 3=0000 0001b << 3=0000 1000b. Thus, bit 3 is1and others are0. When we do XOR (^) withregonly bit 4 is flipped and keeps others the same. - [A]
memsetreplacesGoodwithXXXX.buffer+3pointers to the lastX.strncpyreplacesXand space withNo. - [D] In A and C,
stris never incremented resulting in an infinite loop. In B,stris incremented inwhile, thus skipping the check on the first character. It's interesting to note that if'\0'character is passed tostrchras second argument, it will get matched since the first argument ends with the same character. See https://en.cppreference.com/c/string/byte/strchr - [B] The operator
==when used on strings only compares the pointer values. To compare string values, usestrcmp,strncmpor evenmemcmpinstead. - [B] Although
Studentappears to take15 + 15 + 4 = 34bytes, there's something called memory alignment. Sinceageis 4 bytes, computer architecture expects its location to be a multiple of 4. But15 + 15 = 30is not a multiple of 4. Hence, 2 bytes are padded (and never used) afterlast_nameso thatagestarts at a 4-byte boundary. This can be verified by printing the locations of the members ofStudent,printf("%p %p %p", cse_class[0].first_name, cse_class[0].last_name, &cse_class[0].age). You can also check usingsizeof(Student).