C Programs General

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 47

General Questions in C Declarations and Initializations

1. Which of the following statements should be used to obtain a remainder after dividing 3.14 by 2.1 ? A. B. C. D. rem = 3.14 % 2.1; rem = modf(3.14, 2.1); rem = fmod(3.14, 2.1); Remainder cannot be obtain in floating point division.

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.
Example:

#include <stdio.h> #include <math.h> int main () { printf ("fmod of 3.14/2.1 is %lf\n", fmod (3.14,2.1) ); return 0; }

Output: fmod of 3.14/2.1 is 1.040000 View Answer Online Compiler Report Discuss in Forum 2.

What are the types of linkages? A. C. Internal and External External and None B. D. External, Internal and None Internal

Answer & Explanation

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. View Answer Online Compiler Report Discuss in Forum

3.

Which of the following special symbol allowed in a variable name? A. C. * (asterisk) - (hyphen) B. D. | (pipeline) _ (underscore)

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 View Answer Online Compiler Report Discuss in Forum

4.

Is there any difference between following declarations? 1 : extern int fun(); 2 : int fun(); A. B. Both are identical No difference, except extern int fun(); is probably in another file

C. D.

int fun(); is overrided with extern int fun();


None of these

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. View Answer Online Compiler Report Discuss in Forum

5.

How would you round off a value from 1.66 to 2.0? A. C. ceil(1.66) roundup(1.66) B. D. floor(1.66) roundto(1.66)

Answer & Explanation

Answer: Option A Explanation:

/* Example for ceil() and floor() functions: */ #include<stdio.h> #include<math.h> int main() { printf("\n Result : %f" , ceil(1.44) ); printf("\n Result : %f" , ceil(1.66) ); printf("\n Result : %f" , floor(1.44) ); printf("\n Result : %f" , floor(1.66) ); return 0; } // Output: // Result : 2.000000 // Result : 2.000000 // Result : 1.000000

// Result : 1.000000

6.

By default a real number is treated as a A. C. float long double B. D. double 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 thedouble 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. View Answer Online Compiler Report Discuss in Forum 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. C.

1 3

B. D.

2 Both 1 and 2

Answer & Explanation

Answer: Option B Explanation: C data types classification are 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.) View Answer Online Compiler Report Discuss in Forum

8.

Is the following statement a declaration or definition?

extern int i;
A. C. Declaration Function B. D. Definition 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". View Answer Online Compiler Report Discuss in Forum

9.

Identify which of the following are declarations

1 : extern int x; 2 : float square ( float x ) { ... } 3 : double pow(double, double); A. C. 1 1 and 3 B. D. 2 3

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. View Answer Online Compiler Report Discuss in Forum

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

extern int a is declaration, int a = 20 is the definition int a = 20 is declaration, extern int a is the definition int a = 20 is definition, a is not defined 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? A. C. Defining Prototyping B. D. Declaring 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.

Floating Point Issues


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

Answer & Explanation

Answer: Option C Explanation: The floating point data types are called real data types. Hence float, double, andlong double are real data types. View Answer Online Compiler Report Discuss in Forum 2. What will you do to treat the constant 3.14 as a long double? A. C. use 3.14LD use 3.14DL B. D. use 3.14L 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) View Answer Online Compiler Report Discuss in Forum

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 (on intel machine)?

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

Answer & Explanation

Answer: Option C Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum

4.

Which of the following range is a valid long double ? A. C. 3.4E-4932 to 1.1E+4932 1.1E-4932 to 1.1E+4932 B. D. 3.4E-4932 to 3.4E+4932 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 View Answer Online Compiler Report Discuss in Forum

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. C. #include<conio.h> #include<stdlib.h> B. D. #include<math.h> #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. C. y = (int)(x + 0.5) y = (int)x + 0.5 B. D. y = int(x + 0.5) 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 totypecast the value of x by using (int)
Example:

#include <stdio.h> int main () { float x = 3.6; int y = (int)(x + 0.5); printf ("Result = %d\n", y ); return 0; }

Output: Result = 4. View Answer Online Compiler Report Discuss in Forum 7.

The binary equivalent of 5.375 is A. C. 101.101110111 101011 B. D. 101.011 None of above

Answer & Explanation

Answer: Option B Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum

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. B. C. ABCD DCBA 0xABCD

D.

Depends on big endian or little endian architecture

Answer & Explanation

Answer: Option D Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum

9.

What will you do to treat the constant 3.14 as a float? A. C. use float(3.14f) use f(3.14) B. D. use 3.14f use (f)(3.14)

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) View Answer Online Compiler Report Discuss in Forum

10. Which of the following statement obtains the remainder on dividing 5.5 by 1.3 ? A. C. rem = (5.5 % 1.3) rem = fmod(5.5, 1.3) B. D. rem = modf(5.5, 1.3) Error: we can't divide

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.
Example:

#include <stdio.h>

#include <math.h> int main () { printf ("fmod of 5.5 by 1.3 is %lf\n", fmod (5.5, 1.3) ); return 0; }

Output: fmod of 5.5 by 1.3 is 0.300000

Pointers
1. What is (void*)0? A. B. C. D. Representation of NULL pointer Representation of void pointer Error None of above

Answer & Explanation

Answer: Option A Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 2.

Can you combine the following two statements into one?

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

Answer & Explanation

Answer: Option C Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 3.

In which header file is the NULL macro defined? A. C. stdio.h stdio.h and stddef.h B. D. stddef.h stdlib.h

Answer & Explanation

Answer: Option C Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 4. How many bytes are occupied by near, far and huge pointers (DOS)? A. C. near=2 far=4 huge=4 near=2 far=4 huge=8 B. D. near=4 far=8 huge=8 near=4 far=4 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. View Answer Online Compiler Report Discuss in Forum 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

6.

What would be the equivalent pointer expression for referring the array elementa[i][j][k][l] A. C. ((((a+i)+j)+k)+l) (((a+i)+j)+k+l) B. D. *(*(*(*(a+i)+j)+k)+l) ((a+i)+j+k+l)

Answer & Explanation

Answer: Option B

ptr[0]------>*(ptr+0); ptr[1]------>*(ptr+1); a[i]---->*(a+i); a[i][j]---->*(*(a+i)+j); a[i][j][k]---->*(*(*(a+i)+j)+k);

7.

A pointer is A. B. C. D. A keyword used to create variables A variable that stores address of an instruction A variable that stores address of other variable All of the above

Answer & Explanation

Answer: Option C Explanation: No answer description available for this question. Let us discuss. View Answer Online Compiler Report Discuss in Forum 8.

The operator used to get value at address stored in a pointer variable is A. * B. &

C.

&&

D.

||

Answer & Explanation

Answer: Option A

Because you can get value stored at any address only through *.& is used to retrieve the address value.!

Structures, Unions, Enums


1. How will you free the allocated memory ? A. C. remove(var-name); delete(var-name); B. D. free(var-name); dalloc(var-name);

Answer & Explanation

Answer: Option B

free(referred location name); free() - built in function to free or clear the memory space. If you use free, the referred memory location released for the future use or other operations 2. What is the similarity between a structure, union and enumeration? A. B. C. D. All of them let you define new values All of them let you define new data types All of them let you define new pointers All of them let you define new structures

Answer: Option B

Structures unions and enumeration is used to create a new datatyppe that holds all kinds of datatype that is it includesn a datatype that can hold int, float, char, array inside a user ndefined datatype.

1.

In which numbering system can the binary number 1011011111000101 be easily converted to? A. C. Decimal system Octal system B. D. Hexadecimal system No need to convert

Answer & Explanation

Answer: Option B Explanation: Hexadecimal system is better, because each 4-digit binary represents one Hexadecimal digit. View Answer Online Compiler Report Discuss in Forum 2.

Which bitwise operator is suitable for turning off a particular bit in a number? A. C. && operator || operator B. D. & operator ! operator

Answer & Explanation

Answer: Option B

Any bit AND(&) with 0 will give a zero .i.e. will turn that particular bit OFF 3. Which bitwise operator is suitable for turning on a particular bit in a number? A. C. && operator || operator B. D. & operator | operator

Answer & Explanation

Answer: Option D

"|" this operator is known as OR operator and we know that with or operator 1| 0 = 1 1| 1 = 1 that's why here we use or operator

4.

Which bitwise operator is suitable for checking whether a particular bit is on or off? A. C. && operator || operator B. D. & operator ! operator

Answer & Explanation

Answer: Option B

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

Answer & Explanation

Answer: Option B

The malloc() and calloc() are two library fuctions to allocate memory in language c. To allocate a block of memory, call malloc & calloc. The prototype for this function is in 'stdlib.h' 2. What function should be used to free the memory allocated by calloc() ? A. C. dealloc(); free(); B. D. malloc(variable_name, 0) memalloc(variable_name, 0)

Answer & Explanation

Answer: Option C

here are only 3 memory allocation functions in C calloc(),malloc() and free(). free is to free that allocated space

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. C. memfree(int p); malloc(p, 0); B. D. dealloc(p); free(p);

Answer & Explanation

Answer: Option D

Any allocation functions like alloc () , malloc () and calloc () should release their memory space with free () function only. 4. Specify the 2 library functions to dynamically allocate memory? A. B. C. D.

malloc() and memalloc() alloc() and memalloc() malloc() and calloc() memalloc() and faralloc()

Answer: Option C The malloc() is used to assign the single block of memory at run time and the calloc() is used to assign the multiple blocks of memory at runtime.

Library Functions
1. What will the function rewind() do? A. B. C. D. Reposition the file pointer to a character reverse. Reposition the file pointer stream to end of file. Reposition the file pointer to begining of that line. 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); View Answer Online Compiler Report Discuss in Forum 2.

Input/output function prototypes and macros are defined in which header file? A. C. conio.h stdio.h B. D. stdlib.h 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. View Answer Online Compiler Report Discuss in Forum

3.

Which standard library function will you use to find the last occurance of a character in a string in C? A. C. strnchar() strrchar() B. D. strchar() strrchr()

Answer & Explanation

Answer: Option D Explanation:

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


Example:

#include <stdio.h>

#include <string.h> int main() { char str[30] = "12345678910111213"; printf("The last position of '2' is %d.\n", strrchr(str, '2') - str); return 0; }

Output: The last position of '2' is 14. View Answer Online Compiler Report Discuss in Forum

4.

What is stderr ? A. C. standard error standard error streams B. D. standard error types 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). View Answer Online Compiler Report Discuss in Forum

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: 1. 2. 3. 4.

itoa() converts an integer to a string. ltoa() converts a long to a string. ultoa() converts an unsigned long to a string. sprintf() sends formatted output to a string, so it can be used to convert any type of values

to string type.

#include<stdio.h> #include<stdlib.h> int main(void) { int num1 = 12345; float num2 = 5.12; char str1[20]; char str2[20]; itoa(num1, str1, 10); /* 10 radix value */ printf("integer = %d string = %s \n", num1, str1); sprintf(str2, "%f", num2); printf("float = %f string = %s", num2, str2); return 0; } // Output: // integer = 12345 string = 12345 // float = 5.120000 string = 5.120000

6.

What is the purpose of fflush() function. A. B. C. D. flushes all streams and specified streams. flushes only specified stream. flushes input/output buffer. 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.


View Answer Online Compiler Report Discuss in Forum

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); View Answer Online Compiler Report Discuss in Forum

8.

What will the function randomize() do in Turbo C under DOS? A. B. C. D. returns a random number. returns a random number generator in the specified range. returns a random number generator with a random value based on time. return a random number with a given seed value.

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.

/* Prints a random number in the range 0 to 99 */ #include <stdlib.h> #include <stdio.h> #include <time.h> int main(void) { randomize(); printf("Random number in the 0-99 range: %d\n", random (100)); return 0; }

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. C. Infinite times 0 times B. D. 11 times 10 times

Answer & Explanation

Answer: Option C

The condition x<5 is true until x=5, so the true block executed. The code after the continue Statement (including printf("IndiaBIX");) will not executed and go to increment step(x++). Again the loop is continued. When x=5, then else block executed, it has break statement, So loop is terminated. Here the control never go to printf("IndiaBIX"); statement.

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;

} A. C. Infinite times 256 times B. D. 255 times 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. View Answer Online Compiler Report Discuss in Forum 3.

Which of the following is not logical operator? A. C. & || B. 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. View Answer Online Compiler Report Discuss in Forum

4.

In mathematics and computer programming, which is the correct order of mathematical operators ? A. B. Addition, Subtraction, Multiplication, Division Division, Multiplication, Addition, Subtraction

C. D.

Multiplication, Addition, Division, Subtraction Addition, Division, Modulus, Subtraction

Answer & Explanation

Answer: Option B Explanation: Simply called as BODMAS (Brackets, Order, Division, Multiplication, Addition and Subtraction). Mnemonics are often used to help students remember the rules, but the rules taught by the use of acronyms can be misleading. In the United States the acronym PEMDAS is common. It stands for Parentheses, Exponents, Multiplication, Division, Addition, Subtraction. In other English speaking countries, Parentheses may be called Brackets, or symbols of inclusion and Exponentiation may be called either Indices, Powers or Orders, and since multiplication and division are of equal precedence, M and D are often interchanged, leading to such acronyms as BEDMAS, BIDMAS, BODMAS, BERDMAS, PERDMAS, and BPODMAS. For more info: http://en.wikipedia.org/wiki/Order_of_operations View Answer Online Compiler Report Discuss in Forum

5.

Which of the following cannot be checked in a switch-case statement? A. C. Character Float B. D. Integer 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: case constant-expression2: case constant-expression3: ... ... default : statements 4;

statements 1; statements 2; statements3 ;

}
The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.

Functions
1. The keyword used to transfer control from a function back to the calling function is A. C. switch go back B. D. goto return

Answer & Explanation

Answer: Option D Explanation: The keyword return is used to transfer control from a function back to the calling function. Example:

#include<stdio.h> int add(int, int); /* Function prototype */ int main() { int a = 4, b = 3, c; c = add(a, b); printf("c = %d\n", c); return 0; } int add(int a, int b) { /* returns the value and control back to main() function */ return (a+b); }

Output: c=7 View Answer Online Compiler Report Discuss in Forum 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 */ } 1. KR Notation 2. ANSI Notation 1. ANSI Notation 2. KR Notation 1. Pre ANSI C Notation 2. KR Notation 1. ANSI Notation 2. Pre ANSI Notation

A.

B.

C.

D.

Answer & Explanation

Answer: Option C Explanation: KR Notation means Kernighan and Ritche Notation. View Answer Online Compiler Report Discuss in Forum

3.

How many times the program will print "IndiaBIX" ?

#include<stdio.h>
int main() { printf("IndiaBIX"); main(); return 0; } A. C. Infinite times 65535 times B. D. 32767 times 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.

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. B. C. D. The element will be set to 0. The compiler would report an error. The program may crash if some important data gets overwritten. 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. But the modern compilers will take care of this kind of errors. Example: Run the below program, it will crash in Windows (TurboC Compiler)

#include<stdio.h> int main() { int arr[2]; arr[3]=10; printf("%d",arr[3]); return 0; }

Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (Windows) output. Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better. View Answer Online Compiler Report Discuss in Forum

2.

What does the following declaration mean?

int (*ptr)[10]
A. B. C. D.

ptr is array of pointers to 10 integers ptr is a pointer to an array of 10 integers ptr is an array of 10 integers ptr is an pointer to array

Answer & Explanation

Answer: Option B

*ptr is a pointer and [10] is array is array declaration. 3. In C, if you pass an array as an argument to a function, what actually gets passed? A. B. C. D. Value of elements in array First element of the array Base address of the array 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.

Input / Output
1. In a file contains the line "I am a boy\r\n" then on reading this line into the array strusing fgets(). what will str contain? A. C. "I am a boy\r\n\0" "I am a boy\n\0" B. D. "I am a boy\r\0" "I am a boy"

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" View Answer Online Compiler Report Discuss in Forum 2. What is the purpose of "rb" in fopen() function used below in the code?

FILE *fp; fp = fopen("source.txt", "rb"); A. B. C. D. open "source.txt" in binary mode for reading open "source.txt" in binary mode for reading and writing Create a new file "source.txt" for reading and writing None of above

Answer & Explanation

Answer: Option A Explanation: The file source.txt will be opened in the binary mode. View Answer Online Compiler Report Discuss in Forum

3.

What does fp point to in the program ?

#include<stdio.h>
int main() { FILE *fp; fp=fopen("trial", "r"); return 0;

} A. B. C. D. The first character in the file A structure which contains a char pointer which points to the first character of a file. The name of the file. The last character in the file.

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. View Answer Online Compiler Report Discuss in Forum

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. C. Reading Appending B. D. Writing Read and Write

Answer & Explanation

Answer: Option D Explanation:

r+ Open an existing file for update (reading and writing).


View Answer Online Compiler Report Discuss in Forum

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

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; } A. C. "A.C" "B.C" "C.C" "A.C" B. D. "B.C" "C.C" Error in fclose()

Answer & Explanation

Answer: Option D Explanation: Extra parameter in call to fclose().

View Answer Online Compiler Report Discuss in Forum 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. C. rn err B. D. Trh 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 chand 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 filesource.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. View Answer Online Compiler Report Discuss in Forum

8.

To scan a and b given below, which of the following scanf() statement will you use?

#include<stdio.h>
float a; double b; A. C. scanf("%f %f", &a, &b); scanf("%f %Lf", &a, &b); B. D. scanf("%Lf %Lf", &a, &b); 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); View Answer Online Compiler Report Discuss in Forum

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. View Answer Online Compiler Report Discuss in Forum

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. B. C. D. size of "DUMMY.C" file The handle associated with "DUMMY.C" file Garbage value Error in fileno()

Answer & Explanation

Answer: Option B Explanation:

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.

Typedef
1. In the following code, the P2 is Integer Pointer or Integer?

typedef int *ptr; ptr p1, p2; A. C. Integer Error in declaration B. D. Integer pointer None of above

Answer & Explanation

Answer: Option B

Here we declared ptr as a pointer variable. Using that we can assign two pointers. First one hold the address of Ptr variable. And second one points the address of the first variable. Here the declaration is correct. And p1, p2 are not integers. So B is the answer

2.

In the following code what is 'P'?

typedef char *charp; const charp P; A. C. P is a constant P is character type B. D. P is a character constant None of above

Answer & Explanation

Answer: Option A P is a character constant using pointer.

3.

What is x in the following program?

#include<stdio.h>
int main() { typedef char (*(*arrfptr[3])())[10]; arrfptr x; return 0; } A. B. C. D. x is a pointer x is an array of three pointer x is an array of three function pointers Error in x declaration

Answer & Explanation

Answer: Option C

Explanation:

*(* means pointer to pointer which gives value so arrays of three function pointer

Expressions

1.

Which of the following is the correct order of evaluation for the below expression?

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

Answer & Explanation

Answer: Option A Explanation: C uses left associativity for evaluating expressions to break a tie between two operators having same precedence. View Answer Online Compiler Report Discuss in Forum 2.

Which of the following correctly shows the hierarchy of arithmetic operations in C? A. C. /+*+-/* B. D. *-/+ /*+-

Answer & Explanation

Answer: Option D Explanation: Simply called as BODMAS (Bracket of Division, Multiplication, Addition and Subtraction). How Do I Remember ? BODMAS !

y B - Brackets first y O - Orders (ie Powers and Square Roots, etc.)

y DM - Division and Multiplication (left-to-right) y AS - Addition and Subtraction (left-to-right)


View Answer Online Compiler Report Discuss in Forum

3.

Which of the following is the correct usage of conditional operators used in C? A. C. a>b ? c=30 : c=40; max = a>b ? a>c?a:c:b>c?b:c B. D. a>b ? c=30; 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. View Answer Online Compiler Report Discuss in Forum

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. B. C. D. f1, f2, f3 f3, f2, f1 Order may vary from compiler to compiler 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. View Answer Online Compiler Report Discuss in Forum

5.

Which of the following are unary operators in C? 1. ! 2. sizeof 3. ~ 4. && A. C. 1, 2 2, 4 B. D. 1, 3 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. C. 2134 4321 B. D. 1234 3214

Answer & Explanation

Answer: Option A

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

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. B. C. D. It compiles Compiles with an warning Not compile Compiles and print nothing

Answer & Explanation

Answer: Option C Explanation: The code won't compile since declaration of t cannot occur within parenthesis. View Answer Online Compiler Report Discuss in Forum 2.

In which stage the following code

#include<stdio.h>
gets replaced by the contents of the file stdio.h A. C. During editing During execution B. D. During linking 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.

Strings
1. Which of the following function sets first n characters of a string to a given character? A. C. strinit() strset() B. D. strnset() strcset()

Answer & Explanation

Answer: Option B Explanation: Declaration:

char *strnset(char *s, int ch, size_t n); Sets the first n characters of sto 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 View Answer Online Compiler Report Discuss in Forum

2.

If the two strings are identical, then strcmp() function returns A. C. -1 0 B. D. 1 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 View Answer Online Compiler Report Discuss in Forum

3.

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

Answer & Explanation

Answer: Option D Explanation: The statement printf("\\n"); prints '\n' on the screen. View Answer Online Compiler Report Discuss in Forum

4.

The library function used to find the last occurrence of a character in a string is A. C. strnstr() strrchr() B. D. laststr() strstr()

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. Example:

#include <string.h> #include <stdio.h> int main(void) { char text[] = "I learn through IndiaBIX.com"; char *ptr, c = 'i'; ptr = strrchr(text, c); if (ptr) printf("The position of '%c' is: %d\n", c, ptr-text); else printf("The character was not found\n"); return 0; }

Output: The position of 'i' is: 19 View Answer Online Compiler Report Discuss in Forum

5.

Which of the following function is used to find the first occurrence of a given string in another string? A. C. strchr() strstr() B. D. strrchr() 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. Example:

#include <stdio.h> #include <string.h> int main(void) { char *str1 = "IndiaBIX", *str2 = "ia", *ptr; ptr = strstr(str1, str2); printf("The substring is: %s\n", ptr); return 0; }

Output: The substring is: iaBIX

6.

Which of the following function is more appropriate for reading in a multi-word string? A. C. printf(); gets(); B. D. scanf(); puts();

Answer & Explanation

Answer: Option C Explanation:

gets(); collects a string of characters terminated by a new line from the standard input stream stdin

#include <stdio.h> int main(void) { char string[80]; printf("Enter a string:"); gets(string); printf("The string input was: %s\n", string); return 0;

Output: Enter a string: IndiaBIX The string input was: IndiaBIX View Answer Online Compiler Report Discuss in Forum 7.

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. Example:

#include<stdio.h> int xstrlen(char *s) { int length=0; while(*s!='\0') { length++; s++; } return (length); }

int main() { char d[] = "IndiaBIX"; printf("Length = %d\n", xstrlen(d)); return 0; }

Output: Length = 8

Command Line Arguments


1. The maximum combined length of the command-line arguments including the spaces between adjacent arguments is A. B. C. D. 128 characters 256 characters 67 characters It may vary from one operating system to another

Answer & Explanation

Answer: Option D Because command prompt depend on the Operating System (OS) and when OS change then the value of the command line argument changes.

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

3.

What do the 'c' and 'v' in argv stands for? A. B. C. D. 'c' means argument control 'v' means argument vector 'c' means argument count 'v' means argument vertex 'c' means argument count 'v' means argument vector 'c' means argument configuration 'v' means argument visibility

Answer & Explanation

Answer: Option C

argc -----number of argument. argv------argument if u run the program as "prog a b c" argc==4 argv[0]=prog(executable file)

You might also like