Programming Questions

Practice Programming MCQs with answers and explanations. Page 4 of 6.

Category
Technical Questions
Topic
Programming
Page
4 / 6
Mode
Practice

Questions

Open any question to view the answer and explanation.

In the context of 8086-style segmented memory addressing in C (for example, Turbo C on DOS), how can you obtain the segment and offset parts from a far pointer that holds the address of a memory location?
Open
View answer
In C, consider the following program. Assume that the array arr begins at address 65486 in memory. int main(void) { int arr[] = {12, 14, 15, 23, 45}; printf("%u %u", arr, &arr); return 0; } What values will be printed, given that sizeof(int) is 2 bytes and the implementation prints addresses as unsigned integers?
Open
View answer
In standard C (without compiler-specific extensions), consider the following code: int main(void) { int a = 10, *j; void *k; j = k = &a; j++; k++; printf("%u %u", j, k); return 0; } Would this program compile successfully, and why?
Open
View answer
In a DOS/Turbo C style C program, you want to write the character 'A' directly to video memory at segment B800h, offset 0000h using a far pointer. The following code generates a warning: int main(void) { char far *scr; scr = 0xB8000000; *scr = 'A'; return 0; } How can you eliminate the warning while still assigning the correct far address?
Open
View answer
In C, consider the following program compiled with a typical 16-bit compiler where char is 1 byte and int is 2 bytes: int main(void) { char ch = 'A'; printf("%d%d", sizeof(ch), sizeof('A')); return 0; } What will this program print?
Open
View answer
In C, consider the following typedef declaration for a linked list node pointer: typedef struct { int data; NODEPTR link; } *NODEPTR; What is the error, if any, in this code?
Open
View answer
In C, consider the following code that writes a structure to a file using fwrite: struct emp { char *n; int age; }; struct emp e = { "Sujay", 15 }; FILE *fp; /* assume fp is a valid file pointer opened in binary write mode */ fwrite(&e, sizeof(e), 1, fp); Can another program later call fread on this file to reliably reconstruct the same struct emp value, including the string "Sujay", using a single fread(&e, sizeof(e), 1, fp) call?
Open
View answer
In C, consider the following program compiled on a typical system where string literals are stored in read-only memory: int main(void) { struct emp { char *n; int age; }; struct emp e1 = { "Dravid", 23 }; struct emp e2 = e1; strupr(e2.n); printf("\ %s", e1.n); return 0; } Assuming strupr converts characters of the string in place to uppercase, what will this program print if modifying string literals is (non-portably) allowed by the compiler?
Open
View answer
In C programming, you have two structure variables of the same type. What is a safe and portable way to check whether the contents of these two structures are the same?
Open
View answer
In C, consider the following program that attempts to read and print all lines from a text file: #include "stdio.h" int main(void) { FILE *fp; char str[80]; fp = fopen("trail", "r"); while (!feof(fp)) { fgets(str, 80, fp); puts(str); } fclose(fp); return 0; } What is the logical error, if any, in this code for reading the file?
Open
View answer
In C, consider this program saved as sample.c: int main(int argc, char **argv) { argc = argc - (argc - 1); printf("%s", argv[argc - 1]); return 0; } Regardless of how many command-line arguments are supplied, what does this program print?
Open
View answer
In C, consider the following program run from the command line as: myprog 1 2 3 The source code is: int main(int argc, char *argv[]) { int i, j = 0; for (i = 0; i < argc; i++) j = j + atoi(argv[i]); printf("%d", j); return 0; } What will this program print?
Open
View answer
In C, what will be the output of the following program that uses an unsigned int and the bitwise NOT operator? main() { unsigned int a = 0xffff; /* hexadecimal constant */ ~a; printf("%x", a); }
Open
View answer
In C bit manipulation, consider the following macros for treating a char array as a compact bit set: #define CHARSIZE 8 #define MASK(y) (1 << ((y) % CHARSIZE)) #define BITSLOT(y) ((y) / CHARSIZE) #define SET(x,y) ( (x)[BITSLOT(y)] |= MASK(y) ) #define TEST(x,y) ( (x)[BITSLOT(y)] & MASK(y) ) #define NUMSLOTS(n) (((n) + CHARSIZE - 1) / CHARSIZE) Using these macros, how would you (1) declare an array arr that can hold 50 bits, (2) switch on the 20th bit and (3) test whether the 40th bit is on?
Open
View answer
In the following C code using an incomplete struct type and a typedef for a pointer, is it valid to introduce the typedef before the struct is fully defined, and can the pointer member next use that typedef? typedef struct employee *ptr; struct employee { char name[20]; int age; ptr next; };
Open
View answer
In C, how can you improve the following linked node declaration and allocation code using typedef so that the type name is shorter and clearer? struct node { int data1; float data2; struct node *left; struct node *right; }; struct node *ptr; ptr = (struct node *) malloc(sizeof(struct node));
Open
View answer
In C, how would you dynamically allocate a 2 dimensional array of integers with ROWS rows and COLS columns using pointers and malloc, so that you can access elements as arr[i][j]?
Open
View answer
In the following C program, dynamic memory is allocated for a 2 dimensional array using a double pointer. How should you correctly free all allocated memory to avoid leaks? #include "alloc.h" #define MAXROW 3 #define MAXCOL 4 int main(void) { int **p; int i; p = (int **) malloc(MAXROW * sizeof(int *)); for (i = 0; i < MAXROW; i++) p[i] = (int *) malloc(MAXCOL * sizeof(int)); /* ... use p ... */ }
Open
View answer
In C dynamic memory management, can you increase the size of a dynamically allocated array, and if yes, which standard library function is typically used and how should it be applied safely?
Open
View answer
In C dynamic memory allocation, what is the main difference between malloc() and calloc() functions when allocating blocks from the heap?
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.