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.
A. 4
B. 8
C. 12
D. 16
int a = -7 / 2;A. FFFC
B. 000000FD
C. FFFFFFFC
D. FFFFFFFD
A. 12
B. 24
C. 384
D. 3072
char city[] = "Mumbai";
void *ptr = city;
printf("%s", ptr + 3);A. String bai is printed
B. ptr assignment will compile but with a warning
C. ptr + 3 will result in a compile-time error
D. ptr + 3 will result in a runtime error
int a = 2, b = 5, c = 0;
if (++a || c++) {
if (b-- && ++c) {
}
}A. a:3 b:4 c:2
B. a:3 b:5 c:1
C. a:2 b:4 c:1
D. a:3 b:4 c:1
int i = 10;
{
int i = 5;
i++;
}
printf("%d", i);A. 5
B. 6
C. 10
D. 11
char name[] = "Krishna\0murthy";
printf("%s", name+strlen(name)+1);A. a
B. `` (empty string)
C. m
D. murthy
float xyz[][3] = {
{ 1.2, 4.5, 3.1 },
{ 1.4, 4.2, 2.3 },
{ 2.4, 2.7, 3.9 }
};
printf("%.1f %.1f", (float)sizeof(xyz)+1, (float)*(*xyz+1));A. 13.0 8.0
B. 13.0 5.5
C. 37.0 4.5
D. 37.0 2.4
char city[] = "Mumbai";
printf("%d %d", sizeof(city), strlen(city));A. 7 6
B. 6 7
C. 6 6
D. 7 7
char s1[50] = "hello";
char s2[] = "world";
char *s3 = strcat(strcat(s1, " "), s2);
s3[0] = 'H';
printf("%s; %s; %s", s1, s2, s3);A. Hello world; world; Hello world
B. hello; world; Hello world
C. Hello; world; Hello world
D. hello; helloworld; Helloworld
A. fopen, stdio.h
B. atoi, string.h
C. malloc, stdlib.h
D. log10, math.h
A. name[strlen(name)/2]
B. name[strlen(name)/2+1]
C. name[strlen(name)+1>>1]
D. name[strlen(name)>>2]
int a = 2, b = 4;
int c = ++a+b---a*b;A. -1
B. -2
C. 15
D. -3
A. firstName
B. first_name
C. 1st_name
D. _FIRSTNAME
A. It returns data type FILE *
B. It takes two arguments of types const char* filename and const char mode
C. For read-only file operation mode has to be r
D. To create a new file, mode can be w, w+, a or a+
char const name[] = "Krishna";
char const * q = name;
char * const r = name;
printf("%s %s", q++, r++);A. Build error because q is constant
B. Build error because r is constant
C. Build warning in initialization of q
D. No errors or warnings
17. Suppose we have int main(int argc, char *argv[]) {}. Which of the following statements is correct?
A. argv[argc] is a NULL pointer
B. argv[argc] is a NUL character
C. argv[0] points to the first argument passed to the program
D. argc is zero if program is called without arguments
void main() {
int a = 100;
static int b = a / 2;
}A. b has half the value of a
B. Compile-time warning
C. Compile-time error
D. Runtime error
char *students[] = {
"Akash", "M", "24",
"Divya", "F", "22",
"Praveen", "M", "23"
};A. students is a 2-D array containing strings
B. It's better to write the numbers as integers
C. Variable should be defined as char *students[3] instead
D. Code is fine but could be improved using a struct for each student
int x = 5;
printf("%d %d", x, x++); A. 5 5
B. 6 5
C. 5 6
D. Undefined behaviour
int f1(int a, int b) {
return a > b ? a : -b;
}A. -6
B. 5
C. -5
D. 6
22. We wish to swap two integers a and b. Which of the following function interface will do the job?
A. void swap(int a, int b);
B. void swap(int& x, int& y);
C. void swap(int& a, int& b);
D. void swap(int* x, int* y);
int a = 0x3E8; // hex for 1000
unsigned char *p = (unsigned char*)&a;
printf("%02x %02x", p[0], p[1]);A. 00 00
B. E8 03
C. 03 E8
D. Either A or B
int x[] = { 1, 3, 5, 7, 9 };
int *p = x;
printf("%d %d", *p+1, *(p+3));A. 3 10
B. 2 7
C. 3 7
D. 2 10
typedef struct {
char first_name[15];
char last_name[15];
unsigned int age;
} Student;
int main() {
Student cse_class[] = {
{ "Akash", "Kumar", 18 },
{ "Krishna", "S", 17 },
{ "Ramya", "Verma", 19 }
};
for (Student *p = cse_class; p != NULL; p++) {
printf("Name: %s %s, Age: %d\n",
p->first_name, p->last_name, p->age);
}
return 0;
}A. Build error due to p->
B. Build error because printf call is not on a single line
C. Runtime error due to p++
D. Runtime error due to p != NULL
26. Assume that whole and part are non-NULL non-empty strings. What should TBD be to obtain correct behaviour?
/* This function returns the number of times sub-string part occurs in string whole.
Search is done in a non-overlapping manner.
*/
int count_substr(const char *whole, const char *part) {
int count = 0, offset = 0;
while (1) {
const char *found = strstr(whole+offset, part);
if (found) {
offset = TBD;
count++;
}
else break;
}
return count;
}A. found - whole + strlen(part)
B. whole - part + strlen(part) - 1
C. whole - part + strlen(part)
D. part - whole + strlen(part) - 1
int* list_add(int x[4], int y[4], size_t n) {
int* sum = malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
sum[i] = x[i] + y[i];
}
return sum;
}
int main() {
int a[] = {1, 2, 3, 4};
int b[] = {10, 20, 30, 40};
int *c = list_add(a, b, sizeof(a)/sizeof(int));
}A. size_t n argument is unnecessary since size is available in x and y
B. list_add is constrained to arrays of exactly 4 integers
C. free(sum) should be called before returning from list_add
D. Arrays a and b are passed by reference but not the third argument
A.
FILE *fp = fopen("input.txt", "r");
int lines = 0;
char ch;
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') lines++;
}
printf("%d\n", lines);B.
FILE *fp = fopen("input.txt", "r");
int lines = 0;
int ch;
while ((ch = fgetc(fp)) != EOF) {
if (ch == '\n') lines++;
}
printf("%d\n", lines);C.
FILE *fp = fopen("input.txt", "r");
int lines = 0;
while (!feof(fp)) {
if (fgetc(fp) == '\n') lines++;
}
printf("%d\n", lines);D.
FILE *fp = fopen("input.txt", "r");
int lines = 0;
while (fgets(NULL, 0, fp)) lines++;
printf("%d\n", lines);typedef enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY,
DAY_COUNT
} Day;
const char *day_names[] = {
"MONDAY",
"TUESDAY",
"WEDNESDAY",
"THURSDAY",
"FRIDAY",
"SATURDAY",
"SUNDAY"
};
int main() {
Day today = SATURDAY;
Day three_days_later = (today + 3) TBD;
printf("%s %s", day_names[today], day_names[three_days_later]);
}A. + DAY_COUNT -1
B. - 1
C. % DAY_COUNT
D. % SUNDAY
30. The following code is meant to count non-alphanumeric and non-blank characters. What should TBD be to obtain correct behaviour?
unsigned char address[] = "#12-B, 4th Main Road, 12th Cross, Domlur, Bangalore 560038.";
char *p = address;
unsigned int count = 0;
while (*p) TBD
printf("%d", count);A. { if (!isalnum(*p) && !isblank(*p)) count++; p++; }
B. if (!isalnum(*p) && !isblank(*p++)) count++;
C. if (!isalnum(*p++) && !isblank(*p)) count++;
D. if (!isalnum(*p) && !isblank(*p++)) count++;
- [B] See https://os.mbed.com/handbook/C-Data-Types or https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Primitive-Types
- [D] Division gives the result
-3. Number3is0011in binary. Number-3in Two's Complement is1100 + 1 = 1101 = 0xD. Sinceinttakes 4 bytes, we should left-fill the number with1-bit values for a negative number. - [C] Because of Operator Precedence, this is evaluated in this manner:
10 / 3 << 1 + 2 * sizeof(int) >> 2=3 << 1 + 2 * 4 >> 2=3 << 1 + 8 >> 2=3 << 9 >> 2=0011b << 9 >> 2=0011 0 0000 0000b >> 2=0011 0 0000 00b=0001 1000 0000b=0x180=384. Between,<<and>>, the evaluation is left-to-right order. Hence, left shift happens first before right shift. - [A] C language allows implicit conversion to and from a
void*. So there's won't be any warning forptrassignment. But forptr + 3, some compilers will give a warning if the right compilation flags are enabled. Most commonly, compiler will move the pointer by three bytes. - [D] Because of lazy evaluation,
c++is not executed since left-side expression of||is already true. - [C] The second
ihas a limited scope only within the{}code block. Functionprintfdeals with theifrom the outer scope. - [D]
name+strlen(name)advances the pointer to the null character.+1moves the pointer to themcharacter, from where the remaining string is printed. - [C] Each row takes 12 bytes.
xyztakes 36 bytes.*xyzdereferences the 2-D matrix to the first row.+1moves the pointer to second item of the row. Then we dereference it to obtain the value4.5. This is equivalent toxyz[0][1], which is more readable. - [A]
sizeofincludes the NUL string-terminating character. - [A] In fact,
s3is redundant since it points tos1.strcatmodifies the first argument in-place but the first argument is also returned by the function. Hence, the return ofstrcat(s1, " ")can be passed directly as first argument of the outerstrcatcall. - [B]
atoifunction is in header filestdlib.h. - [A] For example, if
name="Krishna"(7 characters),strlen(name)/2 = 7/2 = 3. Since indexing is from zero,name[3]is the middle characters. Another way to access the middle character isname[strlen(name)>>1]. - [B] Compiler parses the expression left to right as
(++a) + (b--) - (a*b), which is3 + 4 - 3*3=-2. Note that due to postfix decrement, two different values ofbare used. - [C] Identifiers (variable, function or macro names) can't start with a digit (0-9) though digits are allowed in non-starting positions.
- [B] Second argument is
const char* mode. It has to be a string, not a character. This is evident inw+, which has two characters. See https://en.cppreference.com/c/io/fopen - [B] There are no warnings or errors due to
qbutrhas both. In initialization ofr, there's a warning since the constant nature of the value is discarded. Only therpointer is a constant, not what it points to. Becauseris a constant pointer, it can't be incremented. - [A]
char *argv[]is actually an array of pointers to characters. It's equivalent tochar **argv. C standard requires thatargv[argc]be initialized toNULL.argv[0]points to the program name, including the path if called that way.argchas a minimum value of1sinceargv[0]is always populated. - [C]
staticrequires compile-time initialization butais evaluated only at runtime even though100is a constant. - [D]
studentsis 1-D array containing strings. Since we can't mix different types in a single array, everything is treated as strings.char *students[3]won't work since it expects only three strings but we're initializing with nine strings. Astructwill give names for each data part (eg.name,sex,age) and make the code more readable. - [D] C language doesn't impose any constraint on compiler on the order of evaluating the arguments to a function. If they're evaluation left-to-right, we would get
5 5; else we would get6 5. Hence, it's better to avoid such code. - [A]
f1(2, 4)returns-4andf1(-2, -3)returns-2. - [D] The integers have to be passed by reference, not passed by value as shown in option A. The caller has to call the function as
swap(&a, &b);. The argument names used in the function can be anything. Options B and C use invalid syntax, though such a syntax is allowed in C++ language. - [D] Option B on Little-Endian machines. Option A on Big-Endian machines since the
03is atp[2]andE8is atp[3]. This difference due to Endianness means that such code is not portable across machines and therefore must be avoided. - [B] With
*p+1value is incremented. With*(p+3)pointer is moved and then value is obtained. - [D]
p != NULLis useless to identify the end of the array of students. Instead usep - cse_class != sizeof(cse_class)/sizeof(cse_class[0]). Note thatp - cse_classreturns the offset in terms ofStudent, not bytes. - [A] Once a match is found, we advance
offsetby the string length ofpartsince searches are done in a non-overlapping manner. For overlapping searches, we can dooffset = found - whole + 1;. - [D] We can do
sizeof(a)inmainsince the entire array is in the current scope. However, inlist_add, only pointers toaandbare available. Hence, we can't dosizeof(x)inlist_add. Thereforesize_t nargument is needed. The syntaxint x[4]is equivalent toint x[]andint *x. The value4has no importance to the compiler.free(sum)should be called inmain. - [B]
fgetcreturns anint.EOFis also anint.feofshould be called after a call to another I/O function. See https://en.cppreference.com/c/io - [C]
enumvalues are in fact integers, by default starting from value0. Hence,SUNDAYis6andDAY_COUNTis7. Theday_namesarray can be indexed in the range 0-6. - [A] Pointer increment is placed outside the
ifso that it's not bypassed due to lazy evaluation. Since it's outside,{}is used to force it to be part of thewhilestatement.