76
C Programming :: Declarations and Initializations 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 B . External, Internal and None C . External and None D . Internal Answer: Option B Explanation: External Linkage-> means global, non-static variables and functions. Internal Linkage-> means static variables and functions with file

C Programming

Embed Size (px)

Citation preview

Page 1: C Programming

C Programming :: Declarations and Initializations1. 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 B.External, Internal and None

C.External and None D.Internal

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)

Answer & Explanation

Answer: Option D

Explanation:

Variable names in C are made up of letters (upper and lower case) and digits. The underscore

D

Page 2: C Programming

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

Answer & ExplanationAnswer: Option BExplanation: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)

Answer & Explanation

Answer: Option A

B

A

Page 3: C Programming

6. By default a real number is treated as a

A.float B.double

C.long double D.far double

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.

7. Which of the following is not user defined data type?

1 :

struct book{ char name[10]; float price; int pages;};

2 : long int l = 2.35;

3 : enum day {Sun, Mon, Tue, Wed};A.1 B.2C.3 D.Both 1 and 2

Answer: Option B

Explanation:

C data types classification are

1. Primary data types 1. int 2. char 3. float

B

Page 4: C Programming

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

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 B.2

C.1 and 3

D.3

A

C

Page 5: C Programming

Answer & Explanation

Answer: Option C

Explanation:

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 definitionC.int a = 20 is definition, a is not definedD.

a is declared, a is not defined

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

A

Page 6: C Programming

.C.Prototyping D.Calling

Answer & Explanation

Answer: Option B

Explanation:

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.

C Programming :: Floating Point Issues

1. What are the different types of real data type in C ?A.float, double B.short int, double, long intC.float, double, long double D.double, long int, float

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.14LC.use 3.14DL D.use 3.14LF

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)

B

C

B

Page 7: C Programming

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 B.04 CA 00 00C.00 00 AC 40 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

C.1.1E-4932 to 1.1E+4932 D.1.7E-4932 to 1.7E+4932

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?

C

A

Page 8: C Programming

#include<stdio.h>int main(){ printf("%f\n", log(36.0)); return 0;}

A.#include<conio.h> B.#include<math.h>C.#include<stdlib.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 isA.y = (int)(x + 0.5) B.y = int(x + 0.5)C.y = (int)x + 0.5 D.y = (int)((int)x + 0.5)

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)

7. The binary equivalent of 5.375 is

A.101.101110111 B.101.011

C.101011 D.None of above

B

A

B

Page 9: C Programming

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.ABCDB.DCBAC.0xABCDD.Depends on big endian or little endian architecture

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)

Answer & ExplanationAnswer: Option BExplanation: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

D

B

Page 10: C Programming

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 :: Pointers1. What is (void*)0?

A.Representation of NULL pointerB.Representation of void pointerC.ErrorD.None of above

Answer & Explanation

Answer: Option A

Explanation:

No answer description available for this question

2.Can you combine the following two statements into one?

char *p;p = (char*) malloc(100);

A.

char p = *malloc(100);

B.char *p = (char) malloc(100);

C

A

2

Page 11: C Programming

C.char *p = (char*)malloc(100);D.

char *p = (char *)(malloc*)(100);

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question.

3. In which header file is the NULL macro defined?A.stdio.h B.stddef.hC.stdio.h and stddef.h 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

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.

C

C

A

Page 12: C Programming

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.'->'

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) D.((a+i)+j+k+l)

Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question

7. A pointer isA.A keyword used to create variablesB.A variable that stores address of an instructionC.A variable that stores address of other variableD.All of the above

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question

D

B

C

Page 13: C Programming

8. The operator used to get value at address stored in a pointer variable isA.* B.&C.&& D.||

Answer & Explanation

Answer: Option A

Explanation:

No answer description available for this question

C Programming :: Structures, Unions, Enums

1. How will you free the allocated memory ?

A.remove(var-name); B.free(var-name);

C.delete(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 valuesB.All of them let you define new data typesC.All of them let you define new pointersD.All of them let you define new structures

Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question

A

B

B

Page 14: C Programming

C Programming :: Bitwise Operators

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

Answer & Explanation

Answer: Option B

Explanation:

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

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.& operatorC.|| operator D.| operator

Answer & Explanation

Answer: Option D

B

B

D

Page 15: C Programming

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

Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question.

C Programming :: Memory Allocation

1. Which header file should be included to use functions like malloc() and calloc()?A.memory.h B.stdlib.hC.string.h D.dos.h

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)

Answer & Explanation

B

B

C

Page 16: C Programming

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;}

A.memfree(int p); B.dealloc(p);

C.malloc(p, 0); D.free(p);

Answer & Explanation

Answer: Option D

Explanation:

No answer description available for this question

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()

Answer & Explanation

Answer: Option C

D

C

Page 17: C Programming

Explanation:

No answer description available for this question

C Programming :: Library Functions

1. What will the function rewind() do?A.Reposition the file pointer to a character reverse.B.Reposition the file pointer stream to end of file.C.Reposition the file pointer to begining of that line.D.Reposition the file pointer to begining of file.

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

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

D

C

Page 18: C Programming

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()

Answer & Explanation

Answer: Option D

Explanation:

strrchr() returns a pointer to the last occurrence of character in a string.

4. What is stderr ?A.standard error B.standard error types

C.standard error streams

D.standard error definitions

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

Answer & Explanation

Answer: Option A

Explanation:

The function sprintf() can be used for this purpose.

D

C

A

Page 19: C Programming

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.

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

Answer & Explanation

Answer: Option A

Explanation:

Do like this fprintf(stdout, "%s %d %f", str, i, 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.

Answer & Explanation

A

A

C

Page 20: C Programming

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.

C Programming :: Control Instructions

1. How many times "IndiaBIX" is get printed?

#include<stdio.h>int main(){ int x; for(x=-1; x<=10; x++) { if(x < 5) continue; else break; printf("IndiaBIX"); } return 0;}

A.Infinite times B.11 timesC.0 times 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(){

C

Page 21: C Programming

int j=1; while(j <= 255) { printf("%c %d\n", j, j); j++; } return 0;}

A.Infinite times B.255 timesC.256 times D.254 times

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.

3. Which of the following is not logical operator?A.& B.&&C.|| D.!

Answer & Explanation

Answer: Option A

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, ModulusB.Division, Multiplication, Subtraction

B

A

Page 22: C Programming

C.Modulus, Addition, DivisionD.Addition, Division, Modulus, Subtraction

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.IntegerC.Float D.enum

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: statements 1; case constant-expression2: statements 2; case constant-expression3: statements3 ; ... ... 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 isA.switch B.gotoC.go back D.retur

B

C

Page 23: C Programming

n

Answer & Explanation

Answer: Option D

Explanation:

The keyword return is used to transfer control from a function back to the calling function.

2. What is the notation for following functions?

1. int f(int a, float b) { /* Some code */ }

2. int f(a, b) int a; float b; { /* Some code */ }

A.1. KR Notation 2. ANSI Notation

B.1. Pre ANSI C Notation 2. KR Notation

C.1. ANSI Notation 2. KR Notation

D.1. ANSI Notation 2. Pre ANSI Notation

Answer & Explanation

Answer: Option C

Explanation:

KR Notation means Kernighan and Ritche Notation.

3. How many times the program will print "IndiaBIX" ?

#include<stdio.h>

int main(){ printf("IndiaBIX"); main();

D

C

Page 24: C Programming

return 0;}

A.Infinite times B.32767 timesC.65535 times D.Till stack doesn't overflow

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 integersB.ptr is a pointer to an array of 10 integers

D

C

Page 25: C Programming

C.ptr is an array of 10 integersD.ptr is an pointer to array

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 arrayB.First element of the arrayC.Base address of the arrayD.Address of the last element of array

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.

C Programming :: Input / Output

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"

Answer & Explanation

Answer: Option C

B

C

C

Page 26: C Programming

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 readingB.open "source.txt" in binary mode for reading and writingC.Create a new file "source.txt" for reading and writingD.None of above

Answer & Explanation

Answer: Option A

Explanation:

The file source.txt will be opened in the binary mode.

3. What does fp point to in the program ?

#include<stdio.h>

int main(){ FILE *fp; fp=fopen("trial", "r"); return 0;}

A.The first character in the fileB.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.

Answer & Explanation

A

B

Page 27: C Programming

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 B.Writing

C.Appending D.Read and Write

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;

A.printf("%f %lf", a, b);

B.printf("%Lf %f", a, b);C.printf("%Lf %Lf", a, b);D.printf("%f %Lf", a, b);

Answer & Explanation

Answer: Option A

Explanation:

D

A

Page 28: C Programming

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;}

A."A.C" "B.C" "C.C" B."B.C" "C.C"

C."A.C" D.Error in fclose()

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) {

D

Page 29: C Programming

ch=getc(fs); if(ch==EOF) break; else { fseek(fs, 4L, SEEK_CUR); fputc(ch, ft); } } return 0;}

A.r n B.TrhC.err D.None of above

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;

B

Page 30: C Programming

A.scanf("%f %f", &a, &b); B.scanf("%Lf %Lf", &a, &b);C.scanf("%f %Lf", &a, &b); D.scanf("%f %lf", &a, &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()

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.

10. Consider the following program and what will be content of t?

#include<stdio.h>

int main(){ FILE *fp; int t; fp = fopen("DUMMY.C", "w"); t = fileno(fp); printf("%d\n", t); return 0;}

A.

size of "DUMMY.C" file

B.The handle associated with "DUMMY.C" file

D

B

Page 31: C Programming

C.Garbage valueD.

Error in fileno()

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;

A.Integer B.Integer pointer

C.Error in declaration D.None of above

Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question.

2. In the following code what is 'P'?

typedef char *charp;const charp P;

A.P is a B.P is a character constant

B

B

Page 32: C Programming

constantC.P is character type D.None of above

Answer & Explanation

Answer: Option A

Explanation:

No answer description available for this question

3. What is x in the following program?

#include<stdio.h>

int main(){ typedef char (*(*arrfptr[3])())[10]; arrfptr x; return 0;}

A.x is a pointerB.x is an array of three pointerC.x is an array of three function pointersD.Error in x declaration

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question.

C Programming :: Variable Number of Arguments

1. What will be the output of the program?

#include<stdio.h>#include<stdarg.h>void fun(char *msg, ....);

int main(){

A

C

Page 33: C Programming

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 B.1C.4 D.7

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question

2. What will be the output of the program?

#include<stdio.h>#include<stdarg.h>void fun1(char, int, int *, float *, char *);void fun2(char ch, ...);void (*p1)(char, int, int *, float *, char *);void (*p2)(char ch, ...);

int main(){ char ch='A'; int i=10; float f=3.14; char *p="Hello"; p1=fun1; p2=fun2; (*p1)(ch, i, &i, &f, p); (*p2)(ch, i, &i, &f, p); return 0;}void fun1(char ch, int i, int *pi, float *pf, char *p){ printf("%c %d %d %f %s \n", ch, i, *pi, *pf, p);}void fun2(char ch, ...){ int i, *pi; float *pf; char *p; va_list list;

C

Page 34: C Programming

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);}

A.A 10 3.14A 10 3.14

B.A 10 10 3.140000 HelloA 10 10 3.140000 Hello

C.A 10 HelloA 10 Hello

D.Error

Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question

3. What will be the output of the program?

#include<stdio.h>#include<stdarg.h>void dumplist(int, ...);

int main(){ dumplist(2, 4, 8); dumplist(3, 6, 9, 7); return 0;}void dumplist(int n, ...){ va_list p; int i; va_start(p, n);

while(n -> 0) { i = va_arg(p, int); printf("%d", i); } va_end(p);}

A.2 4 B.2 4 8

B

Page 35: C Programming

3 6 3, 6, 9, 7

C.4 86 9 7

D.1 1 11 1 1 1

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question

4. What will be the output of the program?

#include<stdio.h>#include<stdarg.h>void display(int num, ....);

int main(){ display(4, 'A', 'B', 'C', 'D'); return 0;}void display(int num, ...){ char c, c1; int j; va_list ptr, ptr1; va_start(ptr, num); va_start(ptr1, num); for(j=1; j<=num; j++) { c = va_arg(ptr, int); printf("%c", c); c1 = va_arg(ptr1, int); printf("%d\n", c1); }}

A.

A, AB, BC, CD, D

B.

A, aB, bC, cD, d

C.

A, 65B, 66C, 67D, 68

D.

A, 0B, 0C, 0C, 0

Answer & Explanation

C

C

Page 36: C Programming

Answer: Option C

Explanation:

No answer description available for this question.

5. What will be the output of the program?

#include<stdio.h>#include<stdarg.h>void fun1(int num, ....);void fun2(int num, ....);

int main(){ fun1(1, "Apple", "Boys", "Cats", "Dogs"); fun2(2, 12, 13, 14); return 0;}void fun1(int num, ...){ char *str; va_list ptr; va_start(ptr, num); str = va_arg(ptr, char *); printf("%s", str);}void fun2(int num, ...){ va_list ptr; va_start(ptr, num); num = va_arg(ptr, int); printf("%d", num);}

A.Dogs 12 B.Cats 14

C.Boys 13 D.Apple 12

Answer & Explanation

Answer: Option D

Explanation:

No answer description available for this question

D

Page 37: C Programming

C Programming :: Expressions1. Which of the following is the correct order of evaluation for the below expression?

z = x + y * z / 4 % 2 - 1A.* / % + - = B.= * / % + -C./ * % - + = D.* % / - + =

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./ * + -

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)

A

D

C

Page 38: C Programming

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, f3B.f3, f2, f1

C.Order may vary from compiler to compiler

D.None of above

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.

5. Which of the following are unary operators in C?

1. !2. sizeof3. ~4. &&

C

Page 39: C Programming

A.1, 2 B.1, 3C.2, 4 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. Relational2. Arithmetic3. Logical4. Assignment

A.2134

B.1234

C.4321 D.3214

Answer & Explanation

Answer: Option A

Explanation:

1. Arithmetic operators: *, /, %, +, - 2. Relational operators: >, <, >=, <=, ==, !=3. Logical operators : !, &&, ||4. Assignment operators: =

C Programming :: C Preprocessor

D

A

Page 40: C Programming

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 compilesB.Compiles with an warningC.Not compileD.Compiles and print nothing

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.hA.During editing B.During linking

C.During execution D.During preprocessing

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

C

D

Page 41: C Programming

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()

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;}

Output:

string before strnset: abcdefghijklmnopqrstuvwxyz

string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz

2. The library function used to reverse a string is

A.strstr() B.strrev()

C.revstr() D.strreverse()

Answer & Explanation

Answer: Option B

Explanation:

B

B

Page 42: C Programming

strrev(s) Reverses all characters in s

3. If the two strings are identical, then strcmp() function returnsA.-1 B.1C.0 D.Yes

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

4. How will you print \n on the screen?A.printf("\n"); B.echo "\\n";C.printf('\n'); D.printf("\\n");

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 isA.strnstr() B.laststr()C.strrchr() D.strstr()

C

D

C

Page 43: C Programming

Answer & Explanation

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()

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

Page 44: C Programming

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?

A.

int xstrlen(char *s){ int length=0; while(*s!='\0') { length++; s++; } return (length);}

B.

int xstrlen(char s){ int length=0; while(*s!='\0') length++; s++; return (length);}

C.

int xstrlen(char *s){ int length=0; while(*s!='\0') length++; return (length);}

D.

int xstrlen(char *s){ int length=0; while(*s!='\0') s++; return (length);}

Answer & Explanation

Answer: Option A

Explanation:

Option A is the correct function to find the length of given string.

C Programming :: Command Line Arguments

1. The maximum combined length of the command-line arguments including the spaces between adjacent arguments isA.128 characters

C

A

Page 45: C Programming

B.256 charactersC.67 charactersD.It may vary from one operating system to another

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) B.int main(argc, argv)int argc; char *argv;

C.

int main(){ int argc; char *argv;}

D.None of above

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 vectorB.'c' means argument count 'v' means argument vertexC.'c' means argument count 'v' means argument vectorD.'c' means argument configuration 'v' means argument visibility

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question

D

A

C

Page 46: C Programming

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 B.Garbage valueC.Error D.0

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"

2. What will be the output of the program?

#include<stdio.h>#include<stdlib.h>

union employee{ char name[15]; int age; float salary;};const union employee e1;

int main()

A

Page 47: C Programming

{ strcpy(e1.name, "K"); printf("%s %d %f", e1.name, e1.age, e1.salary); return 0;}

A.Error: RValue requiredB.Error: cannot convert from 'const int *' to 'int *const'C.Error: LValue required in strcpyD.No error

Answer & Explanation

Answer: Option D

Explanation:

The output will be:

K 75 0.000000

3. What will be the output of the program?

#include<stdio.h>int fun(int **ptr);

int main(){ int i=10; const int *ptr = &i; fun(&ptr); return 0;}int fun(int **ptr){ int j = 223; int *temp = &j; printf("Before changing ptr = %5x\n", *ptr); const *ptr = temp; printf("After changing ptr = %5x\n", *ptr); return 0;}

A.Address of iAddress of j

B.10223

C.Error: cannot convert parameter 1 from 'const int **' to 'int **'

D.Garbage value

D

Page 48: C Programming

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question

4. What will be the output of the program?

#include<stdio.h>

int main(){ const int x=5; const int *ptrx; ptrx = &x; *ptrx = 10; printf("%d\n", x); return 0;}

A.5 B.10

C.Error

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;

C

C

Page 49: C Programming

5. What will be the output of the program?

#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=30D.Garbage value

Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question

6. What will be the output of the program?

#include<stdio.h>

int main(){ const char *s = ""; char str[] = "Hello"; s = str; while(*s) printf("%c", *s++);

return 0;}

A.Error B.HC.Hello D.Hel

B

Page 50: C Programming

Answer & Explanation

Answer: Option C

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".

7. What will be the output of the program?

#include<stdio.h>int get();

int main(){ const int x = get(); printf("%d", x); return 0;}int get(){ return 20;}

A.Garbage value B.ErrorC.20 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

C

C

Page 51: C Programming

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".

8. What will be the output of the program?

#include<stdio.h>

int fun(int *f){ *f = 10; return 0;}int main(){ const int arr[5] = {1, 2, 3, 4, 5}; printf("before modification arr[3] = %d", arr[3]); fun(&arr[3]); printf("After modification arr[3] = %d", arr[3]); return 0;}

A.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

D.Before modification arr[3] = 4 After modification arr[3] = 4

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

A

Page 52: C Programming

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

9. What will be the output of the program?

#include<stdio.h>

int main(){ const int i=0; printf("%d\n", i++); return 0;}

A.10 B.11

C.No output D.Error: ++needs a value

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

D

Page 53: C Programming

an error "Cannot modify a const object".

Because, we cannot modify a const variable.

10. What will be the output of the program?

#include<stdio.h>

int main(){ const c = -11; const int d = 34; printf("%d, %d\n", c, d); return 0;}

A.

Error B.-11, 34

C.11, 34 D.None of these

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

C Programming :: Complicated Declarations

1. Declare the following statement?"An array of three pointers to chars".A.char *ptr[3](); B.char *ptr[3];C.char (*ptr[3])(); D.char **ptr[3];

Answer & Explanation

B

B

Page 54: C Programming

Answer: Option B

Explanation:

No answer description available for this question

2. What do the following declaration signify?

int *ptr[30];

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.

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](); B.char (*ptr)*[3];C.char (*ptr[3])(); D.char (*ptr)[3];

Answer & Explanation

Answer: Option D

Explanation:

No answer description available for this question

4. What do the following declaration signify?

char *arr[10];

B

D

Page 55: C Programming

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.

Answer & Explanation

Answer: Option A

Explanation:

No answer description available for this question

5. What do the following declaration signify?

int (*pf)();

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.

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)

Answer & Explanation

Answer: Option C

Explanation:

A

C

C

Page 56: C Programming

No answer description available for this question

7. What do the following declaration signify?

void *cmp();

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.

Answer & Explanation

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)()

Answer & Explanation

Answer: Option D

Explanation:

No answer description available for this question

9. What do the following declaration signify?

int *f();

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.

C

D

Page 57: C Programming

Answer & Explanation

Answer: Option B

Explanation:

No answer description available for this question

10. What do the following declaration signify?

void (*cmp)();

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 .

Answer & Explanation

Answer: Option D

Explanation:

No answer description available for this question

11. What do the following declaration signify?

char **argv;

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.

Answer & Explanation

Answer: Option B

B

D

B

Page 58: C Programming

Explanation:

No answer description available for this question

12. What do the following declaration signify?

int (*ptr)[30];

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.

Answer & Explanation

Answer: Option A

Explanation:

No answer description available for this question.

13. What do the following declaration signify?

char *scr;

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.

Answer & Explanation

Answer: Option C

Explanation:

No answer description available for this question

A

C

Page 59: C Programming

2