C Programming
C Programming
1. Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ? A.rem = 3.14 % 2.1; B. rem = modf(3.14, 2.1); C. rem = fmod(3.14, 2.1); D.Remainder cannot be obtain in floating point division. Answer: Option C Explanation: fmod(x,y) - Calculates x modulo y, the remainder of x/y. This function is the same as the modulus operator. But fmod() performs floating point divisions.
2. What are the types of linkages? A.Internal and External C. External and None Answer: Option B Explanation:
External Linkage-> means global, non-static variables and functions. Internal Linkage-> means static variables and functions with file scope. None Linkage-> means Local variables. 3. Which of the following special symbol allowed in a variable? A.* (asterisk) B. | (pipeline) C. - (hyphen) D._ (underscore)
D
Answer & Explanation Answer: Option D Explanation: Variable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit.
Examples of valid (but not very descriptive) C variable names: => foo => Bar => BAZ => foo_bar => _foo42 => _ => QuUx
4. Is there any difference between following declarations? 1 : extern int fun(); 2 : int fun(); A.Both are identical B. No difference, except extern int fun(); is probably in another file C. int fun(); is overrided with extern int fun(); D.None of these
B
Answer & Explanation Answer: Option B Explanation: extern int fun(); declaration in C is to indicate the existence of a global function and it is defined externally to the current module or in another file. int fun(); declaration in C is to indicate the existence of a function inside the current module or in the same file.
&&5. How would you round off a value from 1.66 to 2.0? A.ceil(1.66) B. floor(1.66) C. roundup(1.66) D.roundto(1.66)
A
Answer & Explanation Answer: Option B Explanation: In computing, 'real number' often refers to non-complex floating-point numbers. It include both rational numbers, such as 42 and 3/4, and irrational numbers such as pi = 3.14159265... When the accuracy of the floating point number is insufficient, we can use the double to define the number. The double is same as float but with longer precision and takes double space (8 bytes) than float. To extend the precision further we can use long double which occupies 10 bytes of memory space.
1:
2 : long int l = 2.35; 3 : enum day {Sun, Mon, Tue, Wed}; A.1 C. 3 Answer: Option B Explanation: C data types classification are
B. 2 D.Both 1 and 2
1. Primary data types 1. int 2. char 3. float 4. double 5. void 2. Secondary data types (or) User-defined data type 1. Array 2. Pointer
3. Structure 4. Union 5. Enum So, clearly long int l = 2.35; is not User-defined data type. (i.e.long int l = 2.35; is the answer.)
8. Is the following statement a declaration or definition? extern int i; A.Declaration B. Definition C. Function D.Error
A
Answer & Explanation Answer: Option A Explanation: Declaring is the way a programmer tells the compiler to expect a particular type, be it a variable, class/struct/union type, a function type (prototype) or a particular object instance. (ie. extern int i) Declaration never reserves any space for the variable or instance in the program's memory; it simply a "hint" to the compiler that a use of the variable or instance is expected in the program. This hinting is technically called "forward reference".
9. Identify which of the following are declarations 1 : extern int x; 2 : float square ( float x ) { ... } 3 : double pow(double, double); A.1 C. 1 and 3
C
B. 2 D.3
extern int x; - is an external variable declaration. double pow(double, double); - is a function prototype declaration. Therefore, 1 and 3 are declarations. 2 is definition.
10. In the following program where is the variable a getting defined and where it is getting declared?
#include<stdio.h> int main() { extern int a; printf("%d\n", a); return 0; } int a=20;
A.extern int a is declaration, int a = 20 is the definition B. int a = 20 is declaration, extern int a is the definition C. int a = 20 is definition, a is not defined D.a is declared, a is not defined
A
Answer & Explanation Answer: Option A Explanation: - During declaration we tell the datatype of the Variable. - During definition the value is initialized.
11. When we mention the prototype of a function are we defining the function or declaring it? A.Defining B. Declaring C. Prototyping D.Calling
B
A function prototype in C or C++ is a declaration of a function that omits the function body but does specify the function's name, argument types and return type. While a function definition specifies what a function does, a function prototype can be thought of as specifying its interface.
Answer & Explanation Answer: Option C Explanation: The floating point data types are called real data types. Hence float, double, and long double are real data types.
2. What will you do to treat the constant 3.14 as a long double? A.use 3.14LD B. use 3.14L C. use 3.14DL D.use 3.14LF
B
Answer & Explanation Answer: Option B Explanation: Given 3.14 is a double constant. To specify 3.14 as long double, we have to add L to the 3.14. (i.e 3.14L)
3. If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the program?
#include<stdio.h> #include<math.h> int main()
{ float a=5.375; char *p; int i; p = (char*)&a; for(i=0; i<=3; i++) printf("%02x\n", (unsigned char)p[i]); return 0; }
A.40 AC 00 00 C. 00 00 AC 40
C
B. 04 CA 00 00 D.00 00 CA 04
Answer & Explanation Answer: Option C Explanation: No answer description available for this question.
4. Which of the following range is a valid long double ? A.3.4E-4932 to 1.1E+4932 B. 3.4E-4932 to 3.4E+4932 -4932 +4932 C. 1.1E to 1.1E D.1.7E-4932 to 1.7E+4932
A
Answer & Explanation Answer: Option A Explanation: The range of long double is 3.4E-4932 to 1.1E+4932
5. Which statement will you add in the following program to work it correctly?
#include<stdio.h> int main() { printf("%f\n", log(36.0)); return 0; }
A.#include<conio.h> C. #include<stdlib.h>
B
B. #include<math.h> D.#include<dos.h>
Answer & Explanation Answer: Option B Explanation: math.h is a header file in the standard library of C programming language designed for basic mathematical operations. Declaration syntax: double log(double);
6. We want to round off x, a float, to an int value, The correct way to do is A.y = (int)(x + 0.5) B. y = int(x + 0.5) C. y = (int)x + 0.5 D.y = (int)((int)x + 0.5)
A
Answer & Explanation Answer: Option A Explanation: Rounding off a value means replacing it by a nearest value that is approximately equal or smaller or greater to the given number. y = (int)(x + 0.5); here x is any float value. To roundoff, we have to typecast the value of x by using (int)
Answer & Explanation Answer: Option B Explanation: No answer description available for this question. Let us discuss.
8. A float occupies 4 bytes. If the hexadecimal equivalent of these 4 bytes are A, B, C and D, then when this float is stored in memory in which of the following order do these bytes gets stored? A.ABCD B. DCBA C. 0xABCD D.Depends on big endian or little endian architecture
D
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
9. What will you do to treat the constant 3.14 as a float? A.use float(3.14f) B. use 3.14f C. use f(3.14) D.use (f)(3.14)
B
Answer & Explanation Answer: Option B Explanation: Given 3.14 is a double constant. To specify 3.14 as float, we have to add f to the 3.14. (i.e 3.14f)
10. Which of the following statement obtains the remainder on dividing 5.5 by 1.3 ? A.rem = (5.5 % 1.3) B. rem = modf(5.5, 1.3) C. rem = fmod(5.5, 1.3) D.Error: we can't divide
C
Answer & Explanation Answer: Option C Explanation: fmod(x,y) - Calculates x modulo y, the remainder of x/y. This function is the same as the modulus operator. But fmod() performs floating point divisions.
C Programming :: Pointers
1. What is (void*)0? A.Representation of NULL pointer B. Representation of void pointer C. Error D.None of above
A
Answer & Explanation Answer: Option A Explanation: No answer description available for this question
2
A.char p = *malloc(100); B. char *p = (char) malloc(100); C. char *p = (char*)malloc(100); D.char *p = (char *)(malloc*)(100);
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question.
D.stdlib.h
Answer & Explanation Answer: Option C Explanation: No answer description available for this question.
4. How many bytes are occupied by near, far and huge pointers (DOS)? A.near=2 far=4 huge=4 B. near=4 far=8 huge=8 C. near=2 far=4 huge=8 D.near=4 far=8 huge=8
A
Answer & Explanation Answer: Option A Explanation: near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long.
5. If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable? A.'.' B. '&' C. '*' D.'->'
D
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
6. What would be the equivalent pointer expression for referring the array element a[i][j][k][l] A.((((a+i)+j)+k)+l) B. *(*(*(*(a+i)+j)+k)+l)
C. (((a+i)+j)+k+l)
B
D.((a+i)+j+k+l)
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
7. A pointer is A.A keyword used to create variables B. A variable that stores address of an instruction C. A variable that stores address of other variable D.All of the above
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
8. The operator used to get value at address stored in a pointer variable is A.* B. & C. && D.||
A
Answer & Explanation Answer: Option A Explanation: No answer description available for this question
B. free(var-name); D.dalloc(var-name);
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
2. What is the similarity between a structure, union and enumeration? A.All of them let you define new values B. All of them let you define new data types C. All of them let you define new pointers D.All of them let you define new structures
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
1. In which numbering system can the binary number 1011011111000101 be easily converted to? A.Decimal system B. Hexadecimal system C. Octal system D.No need to convert
B
Hexadecimal system is better, because each 4-digit binary represents one Hexadecimal digit.
2. Which bitwise operator is suitable for turning off a particular bit in a number? A.&& operator B. & operator C. || operator D.! operator
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
3. Which bitwise operator is suitable for turning on a particular bit in a number? A.&& operator B. & operator C. || operator D.| operator
D
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
4. Which bitwise operator is suitable for checking whether a particular bit is on or off? A.&& operator B. & operator C. || operator D.! operator
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question.
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
2. What function should be used to free the memory allocated by calloc() ? A.dealloc(); B. malloc(variable_name, 0) C. free(); D.memalloc(variable_name, 0)
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
3. How will you free the memory allocated by the following program?
#include<stdio.h> #include<stdlib.h> #define MAXROW 3 #define MAXCOL 4 int main() { int **p, i, j; p = (int **) malloc(MAXROW * sizeof(int*)); return 0; }
B. dealloc(p); D.free(p);
4. Specify the 2 library functions to dynamically allocate memory? A.malloc() and memalloc() B. alloc() and memalloc() C. malloc() and calloc() D.memalloc() and faralloc()
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
Answer & Explanation Answer: Option D Explanation: rewind() takes the file pointer to the beginning of the file. so that the next I/O operation will take place at the beginning of the file. Example: rewind(FilePointer);
2. Input/output function prototypes and macros are defined in which header file? A.conio.h B. stdlib.h C. stdio.h D.dos.h
C
Answer & Explanation Answer: Option C Explanation: stdio.h, which stands for "standard input/output header", is the header in the C standard library that contains macro definitions, constants, and declarations of functions and types used for various standard input and output operations
3. Which standard library function will you use to find the last occurance of a character in a string in C? A.strnchar() B. strchar() C. strrchar() D.strrchr()
D
Answer & Explanation Answer: Option D Explanation: strrchr() returns a pointer to the last occurrence of character in a string.
Answer & Explanation Answer: Option C Explanation: The standard error(stderr) stream is the default destination for error messages and other diagnostic warnings. Like stdout, it is usually also directed to the output device of the standard console (generally, the screen).
5. Does there any function exist to convert the int or float to a string? A.Yes B.No
A
Answer & Explanation Answer: Option A Explanation: The function sprintf() can be used for this purpose.
6. What is the purpose of fflush() function. A.flushes all streams and specified streams. B. flushes only specified stream. C. flushes input/output buffer. D.flushes file buffer.
A
Answer & Explanation Answer: Option A Explanation: "fflush()" flush any buffered output associated with filename, which is either a file opened for writing or a shell command for redirecting output to a pipe or coprocess. Example: fflush(FilePointer); fflush(NULL); flushes all streams.
7. Can you use the fprintf() to display the output on the screen? A.Yes B.No
A
8. What will the function randomize() do? A.returns a random number. B. returns a random number generator in the specified range. C. returns a random number generator with a random value based on time. D.return a random number with a given seed value.
C
Answer & Explanation Answer: Option C Explanation: The randomize() function initializes the random number generator with a random value based on time. You can try the sample program given below in Turbo-C, it may not work as expected in other compilers.
A.Infinite times
B. 11 times
C. 0 times
C
D.10 times
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
2. How many times the while loop will get executed if a short int is 2 byte wide?
#include<stdio.h> int main() { int j=1; while(j <= 255) { printf("%c %d\n", j, j); j++; } return 0; }
Answer & Explanation Answer: Option B Explanation: The while(j <= 255) loop will get executed 255 times. The size short int(2 byte wide) does not affect the while() loop.
Explanation: Bitwise operators: & is a Bitwise AND operator. Logical operators: && is a Logical AND operator. || is a Logical OR operator. ! is a NOT operator. So, '&' is not a Logical operator.
4. Which is the correct order of mathematical operators ? A.Division, Multiplication, Modulus B. Division, Multiplication, Subtraction C. Modulus, Addition, Division D.Addition, Division, Modulus, Subtraction
B
Answer & Explanation Answer: Option B Explanation: Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction).
5. Which of the following cannot be checked in a switch-case statement? A.Character B. Integer C. Float D.enum
C
Answer & Explanation Answer: Option C Explanation: The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value.
switch( expression ) {
case constant-expression1: case constant-expression2: case constant-expression3: ... ... default : statements 4; }
The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.
C Programming :: Functions
1. The keyword used to transfer control from a function back to the calling function is A.switch B. goto C. go back D.return
D
Answer & Explanation Answer: Option D Explanation: The keyword return is used to transfer control from a function back to the calling function.
2.
Answer & Explanation Answer: Option D Explanation: A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing. A stack overflow occurs when too much memory is used on the call stack. Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.
C Programming :: Arrays
1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array? A.The element will be set to 0. B. The compiler would report an error. C. The program may crash if some important data gets overwritten. D.The array size would appropriately grow.
Answer & Explanation Answer: Option C Explanation: If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer.
2. What does the following declaration mean? int (*ptr)[10] A.ptr is array of pointers to 10 integers B. ptr is a pointer to an array of 10 integers C. ptr is an array of 10 integers D.ptr is an pointer to array
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question.
3. In C, if you pass an array as an argument to a function, what actually gets passed? A.Value of elements in array B. First element of the array C. Base address of the array D.Address of the last element of array
C
Answer & Explanation Answer: Option C Explanation: The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.
1. In a file contains the line "I am a boy\r\n" then on reading this line into the array str using fgets(). what will str contain? A."I am a boy\r\n\0" B. "I am a boy\r\0" C. "I am a boy\n\0" D."I am a boy"
C
Answer & Explanation Answer: Option C Explanation: Declaration: char *fgets(char *s, int n, FILE *stream); fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first. Therefore, the string str contain "I am a boy\n\0"
2. What is the purpose of "rb" in fopen() function used below in the code?
FILE *fp; fp = fopen("source.txt", "rb");
A.open "source.txt" in binary mode for reading B. open "source.txt" in binary mode for reading and writing C. Create a new file "source.txt" for reading and writing D.None of above
A
Answer & Explanation Answer: Option A Explanation: The file source.txt will be opened in the binary mode.
A.The first character in the file B. A structure which contains a char pointer which points to the first character of a file. C. The name of the file. D.The last character in the file.
B
Answer & Explanation Answer: Option B Explanation: The fp is a structure which contains a char pointer which points to the first character of a file.
4. Which of the following operations can be performed on the file "NOTES.TXT" using the below code?
FILE *fp; fp = fopen("NOTES.TXT", "r+");
A.Reading C. Appending
D
Answer & Explanation Answer: Option D Explanation: r+ Open an existing file for update (reading and writing).
5. To print out a and b given below, which of the following printf() statement will you use?
#include<stdio.h> float a=3.14; double b=3.14;
Answer & Explanation Answer: Option A Explanation: To print a float value, %f is used as format specifier. To print a double value, %lf is used as format specifier. Therefore, the answer is printf("%f %lf", a, b);
6. Which files will get closed through the fclose() in the following program?
#include<stdio.h> int main() { FILE *fs, *ft, *fp; fp = fopen("A.C", "r"); fs = fopen("B.C", "r"); ft = fopen("C.C", "r"); fclose(fp, fs, ft); return 0; }
Answer & Explanation Answer: Option D Explanation: Extra parameter in call to fclose().
7. On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
#include<stdio.h> int main() { int i, fss; char ch, source[20] = "source.txt", target[20]="target.txt", t; FILE *fs, *ft; fs = fopen(source, "r"); ft = fopen(target, "w"); while(1) { ch=getc(fs); if(ch==EOF) break; else { fseek(fs, 4L, SEEK_CUR); fputc(ch, ft); } } return 0; }
A.r n C. err
B
Answer & Explanation Answer: Option B Explanation: The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human". Inside the while loop, ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF. if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops. If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt. fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt. The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file.
8. To scan a and b given below, which of the following scanf() statement will you use?
#include<stdio.h> float a; double b;
Answer & Explanation Answer: Option D Explanation: To scan a float value, %f is used as format specifier. To scan a double value, %lf is used as format specifier. Therefore, the answer is scanf("%f %lf", &a, &b);
9. Out of fgets() and gets() which function is safe to use? A.gets() B.fgets()
B
Answer & Explanation Answer: Option B Explanation: Because, In fgets() we can specify the size of the buffer into which the string supplied will be stored.
A.size of "DUMMY.C" file B. The handle associated with "DUMMY.C" file C. Garbage value D.Error in fileno()
B
Answer & Explanation Answer: Option B Explanation: fileno is a macro that returns the file handle for the stream. fp = fopen("DUMMY.C", "w"); A file DUMMY.C is opened in write mode and returns the file pointer to fp t = fileno(fp); returns the handle for the fp stream and it stored in the variable t printf("%d\n", t); It prints the handle number.
C Programming :: Typedef
1. In the following code, the P2 is Integer Pointer or Integer?
typedef int *ptr; ptr p1, p2;
Answer & Explanation Answer: Option B Explanation: No answer description available for this question.
Answer & Explanation Answer: Option A Explanation: No answer description available for this question
A.x is a pointer B. x is an array of three pointer C. x is an array of three function pointers D.Error in x declaration
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question.
#include<stdarg.h> void fun(char *msg, ....); int main() { fun("IndiaBIX", 1, 4, 7, 11, 0); return 0; } void fun(char *msg, ....) { int tot=0; va_list ptr; int num; va_start(ptr, msg); num = va_arg(ptr, int) num = va_arg(ptr, int) printf("%d", num); }
A.IndiaBIX 1 7 11 0 C. 4
C
B. 1 D.7
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
} void fun2(char ch, ...) { int i, *pi; float *pf; char *p; va_list list; printf("%c ", ch); va_start(list, ch); i = va_arg(list, int); printf("%d ", i); pi = va_arg(list, int*); printf("%d ", *pi); pf = va_arg(list, float*); printf("%f ", *pf); p = va_arg(list, char *); printf("%s", p); }
B.
D.Error
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
24 36 48 C. 697 A.
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
A, A B, B A. C, C D, D A, 65 C. B, 66 C, 67
A, a B, b B. C, c D, d A, 0 D.B, 0 C, 0
D, 68
C
C, 0
Answer & Explanation Answer: Option C Explanation: No answer description available for this question.
A.Dogs 12 C. Boys 13
D
B. Cats 14 D.Apple 12
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
C Programming :: Expressions
1. Which of the following is the correct order of evaluation for the below expression? z=x+y*z/4%2-1 A.* / % + - = B. = * / % + C. / * % - + = D.* % / - + =
A
Answer & Explanation Answer: Option A Explanation: C uses left associativity for evaluating expressions to break a tie between two operators having same precedence.
2. Which of the following correctly shows the hierarchy of arithmetic operations in C? A./ + * B. * - / + C. + - / * D./ * + D
Answer & Explanation Answer: Option D Explanation: Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction). How Do I Remember ? BODMAS ! B - Brackets first O - Orders (ie Powers and Square Roots, etc.) DM - Division and Multiplication (left-to-right) AS - Addition and Subtraction (left-to-right)
3. Which of the following is the correct usage of conditional operators used in C? A.a>b ? c=30 : c=40; B. a>b ? c=30; C. max = a>b ? a>c?a:c:b>c?b:c D.return (a>b)?(a:b)
Answer & Explanation Answer: Option C Explanation: Option A: assignment statements are always return in paranthesis in the case of conditional operator. It should be a>b? (c=30):(c=40); Option B: it is syntatically wrong. Option D: syntatically wrong, it should be return(a>b ? a:b); Option C: it uses nested conditional operator, this is logic for finding greatest number out of three numbers.
4. Which of the following is the correct order if calling functions in the below code? a = f1(23, 14) * f2(12/4) + f3(); A.f1, f2, f3 B. f3, f2, f1 C. Order may vary from compiler to compiler D.None of above
C
Answer & Explanation Answer: Option C Explanation: Here, Multiplication will happen before the addition, but in which order the functions would be called is undefined. In an arithmetic expression the parenthesis tell the compiler which operands go with which operators but do not force the compiler to evaluate everything within the parenthesis first.
4. && A.1, 2 C. 2, 4
D
B. 1, 3 D.1, 2, 3
Answer & Explanation Answer: Option D Explanation: An operation with only one operand is called unary operation. Unary operators: ! Logical NOT operator. ~ bitwise NOT operator. sizeof Size-of operator. && Logical AND is a logical operator. Therefore, 1, 2, 3 are unary operators.
6. In which order do the following gets evaluated 1. Relational 2. Arithmetic 3. Logical 4. Assignment A.2134 C. 4321
A
B. 1234 D.3214
Answer & Explanation Answer: Option A Explanation: 1. Arithmetic operators: *, /, %, +, 2. Relational operators: >, <, >=, <=, ==, != 3. Logical operators : !, &&, || 4. Assignment operators: =
C Programming :: C Preprocessor
1. What will the SWAP macro in the following program be expanded to on preprocessing? will the code compile?
#include<stdio.h> #define SWAP(a, b, c)(c t; t=a, a=b, b=t) int main() { int x=10, y=20; SWAP(x, y, int); printf("%d %d\n", x, y); return 0; }
A.It compiles B. Compiles with an warning C. Not compile D.Compiles and print nothing
C
Answer & Explanation Answer: Option C Explanation: The code won't compile since declaration of t cannot occur within parenthesis.
2. In which stage the following code #include<stdio.h> gets replaced by the contents of the file stdio.h A.During editing B. During linking C. During execution D.During preprocessing
D
Answer & Explanation Answer: Option D Explanation: The preprocessor replaces the line #include <stdio.h> with the system header file of that name. More precisely, the entire text of the file 'stdio.h' replaces the #include directive.
C Programming :: Strings
1. Which of the following function sets first n characters of a string to a given character? A.strinit() B. strnset() C. strset() D.strcset()
B
Answer & Explanation Answer: Option B Explanation: Declaration: char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch
#include <stdio.h> #include <string.h> int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; printf("string before strnset: %s\n", string); strnset(string, letter, 13); printf("string after strnset: %s\n", string); return 0; }
2. The library function used to reverse a string is A.strstr() B. strrev() C. revstr() D.strreverse()
B
Answer & Explanation Answer: Option B Explanation: strrev(s) Reverses all characters in s
3. If the two strings are identical, then strcmp() function returns A.-1 B. 1 C. 0 D.Yes
C
Answer & Explanation Answer: Option C Explanation: Declaration: strcmp(const char *s1, const char*s2); The strcmp return an int value that is if s1 < s2 returns a value < 0 if s1 == s2 returns 0 if s1 > s2 returns a value > 0
Answer & Explanation Answer: Option D Explanation: The statement printf("\\n"); prints '\n' on the screen.
5. The library function used to find the last occurrence of a character in a string is A.strnstr() B. laststr() C. strrchr() D.strstr()
C
Answer: Option C Explanation: Declaration: char *strrchr(const char *s, int c); It scans a string s in the reverse direction, looking for a specific character c
6. Which of the following function is used to find the first occurrence of a given string in another string? A.strchr() B. strrchr() C. strstr() D.strnset()
C
Answer & Explanation Answer: Option C Explanation: The function strstr() Finds the first occurrence of a substring in another string Declaration: char *strstr(const char *s1, const char *s2); Return Value: On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1). On error (if s2 does not occur in s1), strstr returns null.
7. Which of the following function is more appropriate for reading in a multi-word string? A.printf(); B. scanf(); C. gets(); D.puts();
Answer & Explanation Answer: Option C Explanation: gets(); collects a string of characters terminated by a new line from the standard input stream stdin
8. Which of the following function is correct that finds the length of a string?
int xstrlen(char *s) { int length=0; while(*s!='\0') A. { length++; s++; } return (length); } int xstrlen(char *s) { int length=0; while(*s!='\0') C. length++; return (length); }
A
int xstrlen(char s) { int length=0; while(*s!='\0') B. length++; s++; return (length); } int xstrlen(char *s) { int length=0; while(*s!='\0') D. s++; return (length); }
Answer & Explanation Answer: Option A Explanation: Option A is the correct function to find the length of given string.
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
2. According to ANSI specifications which is the correct way of declaring main when it receives command-line arguments? A.int main(int argc, char *argv)
int main() { C. int argc; char *argv; }
A
Answer & Explanation Answer: Option A Explanation: No answer description available for this question
3. What do the 'c' and 'v' in argv stands for? A.'c' means argument control 'v' means argument vector B. 'c' means argument count 'v' means argument vertex C. 'c' means argument count 'v' means argument vector D.'c' means argument configuration 'v' means argument visibility
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
C Programming :: Const
1. What will be the output of the program?
#include<stdio.h> int main() { int y=128; const int x=y; printf("%d\n", x); return 0; }
A.128 C. Error
A
Answer & Explanation Answer: Option A Explanation: Step 1: int y=128; The variable 'y' is declared as an integer type and initialized to value "128". Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value. Step 3: printf("%d\n", x); It prints the value of variable 'x'. Hence the output of the program is "128"
return 0; }
A.Error: RValue required B. Error: cannot convert from 'const int *' to 'int *const' C. Error: LValue required in strcpy D.No error
D
Answer & Explanation Answer: Option D Explanation: The output will be: K 75 0.000000
Address of i Address of j 10 B. 223 C. Error: cannot convert parameter 1 from 'const int **' to 'int **' D.Garbage value A.
C
A.5 C. Error
C
B. 10 D.Garbage value
Answer & Explanation Answer: Option C Explanation: Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'. Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer. Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx. Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x. This will result in an error. To change the value of const variable x we have to use *(int *)&x = 10;
#include<stdio.h> int fun(int **ptr); int main() { int i=10, j=20; const int *ptr = &i; printf("i = %5X", ptr); printf("ptr = %d", *ptr); ptr = &j; printf("j = %5X", ptr); printf("ptr = %d", *ptr); return 0; }
A.i= FFE2 ptr=12 j=FFE4 ptr=24 B. i= FFE4 ptr=10 j=FFE2 ptr=20 C. i= FFE0 ptr=20 j=FFE1 ptr=30 D.Garbage value
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
A.Error C. Hello
C
B. H D.Hel
Explanation: Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string. Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and initialized with a string "Hello". Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains the text "Hello". Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the variable s is available and it prints the each character of the variable s. Hence the output of the program is "Hello".
A.Garbage value C. 20
C
B. Error D.0
Answer & Explanation Answer: Option C Explanation: Step 1: int get(); This is the function prototype for the funtion get(), it tells the compiler returns an integer value and accept no parameters. Step 2: const int x = get(); The constant variable x is declared as an integer data type and initialized with the value "20".
The function get() returns the value "20". Step 3: printf("%d", x); It prints the value of the variable x. Hence the output of the program is "20".
Before modification arr[3] = 4 After modification arr[3] = 10 B. Error: cannot convert parameter 1 from const int * to int * C. Error: Invalid parameter Before modification arr[3] = 4 D. After modification arr[3] = 4 A.
A
Answer & Explanation Answer: Option A Explanation: Step 1: const int arr[5] = {1, 2, 3, 4, 5}; The constant variable arr is declared as an integer array and initialized to arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5 Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 4). Step 3: fun(&arr[3]); The memory location of the arr[3] is passed to fun() and arr[3] value is modified to 10.
A const variable can be indirectly modified by a pointer. Step 4: printf("After modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 10). Hence the output of the program is Before modification arr[3] = 4 After modification arr[3] = 10
A.10 C. No output
D
Answer & Explanation Answer: Option D Explanation: This program will show an error "Cannot modify a const object". Step 1: const int i=0; The constant variable 'i' is declared as an integer and initialized with value of '0'(zero). Step 2: printf("%d\n", i++); Here the variable 'i' is increemented by 1(one). This will create an error "Cannot modify a const object". Because, we cannot modify a const variable.
A.Error C. 11, 34
B
Answer & Explanation Answer: Option B Explanation: Step 1: const c = -11; The constant variable 'c' is declared and initialized to value "-11". Step 2: const int d = 34; The constant variable 'd' is declared as an integer and initialized to value '34'. Step 3: printf("%d, %d\n", c, d); The value of the variable 'c' and 'd' are printed. Hence the output of the program is -11, 34
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
A.ptr is a pointer to an array of 30 integer pointers. B. ptr is a array of 30 pointers to integers. C. ptr is a array of 30 integer pointers. D.ptr is a array 30 pointers.
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
3. Declare the following statement? "A pointer to an array of three chars". A.char *ptr[3](); C. char (*ptr[3])();
D
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
A.arr is a array of 10 character pointers. B. arr is a array of function pointer. C. arr is a array of characters. D.arr is a pointer to array of characters.
A
Answer & Explanation Answer: Option A Explanation: No answer description available for this question
A.pf is a pointer to function. B. pf is a function pointer. C. pf is a pointer to a function which return int D.pf is a function of pointer variable.
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question.
6. Declare the following statement? "A pointer to a function which receives an int pointer and returns float pointer". A.float *(ptr)*int; B. float *(*ptr)(int) C. float *(*ptr)(int*) D.float (*ptr)(int)
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
A.cmp is a pointer to an void type. B. cmp is a void type pointer variable. C. cmp is a function that return a void pointer. D.cmp function returns nothing.
C
Answer: Option C Explanation: No answer description available for this question. Let us discuss.
8. Declare the following statement? "A pointer to a function which receives nothing and returns nothing". A.void *(ptr)*int; B. void *(*ptr)() C. void *(*ptr)(*) D.void (*ptr)()
D
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
A.f is a pointer variable of function type. B. f is a function returning pointer to an int. C. f is a function pointer. D.f is a simple declaration of pointer variable.
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
A.cmp is a pointer to an void function type. B. cmp is a void type pointer function. C. cmp is a function that return a void pointer. D.cmp is a pointer to a function which returns void .
D
Answer & Explanation Answer: Option D Explanation: No answer description available for this question
A.argv is a pointer to pointer. B. argv is a pointer to a char pointer. C. argv is a function pointer. D.argv is a member of function pointer.
B
Answer & Explanation Answer: Option B Explanation: No answer description available for this question
A.ptr is a pointer to an array of 30 integer pointers. B. ptr is a array of 30 integer function pointer. C. ptr is a array of 30 integer pointers. D.ptr is a array 30 pointers.
A
A.scr is a pointer to pointer variable. B. scr is a function pointer. C. scr is a pointer to char. D.scr is a member of function pointer.
C
Answer & Explanation Answer: Option C Explanation: No answer description available for this question
2