c programs general

Upload: vinesh-kumar

Post on 13-Jul-2015

109 views

Category:

Documents


0 download

TRANSCRIPT

General Questions in C Declarations and Initializations1. 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 #include 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 isdefined 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 moduleor 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 #include 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?

#includeint 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 Issues1. 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 #includeint main() { float a=5.375; char *p; int i; p = (char*)&a; for(i=0; i'

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, Enums1. 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 Allocation1. 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 #include #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 Functions1. 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 willtake 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 librarythat 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

#include 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 #include 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 #include #include int main(void) { randomize(); printf("Random number in the 0-99 range: %d\n", random (100)); return 0; }

Control Instructions1. How many times "IndiaBIX" is get printed?

#includeint main() { int x; for(x=-1; xb ? 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: >, =,