C Programming:: Declarations and Initializations: A - B. C. D

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

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

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

2.  What are the types of linkages?


A
Internal and External B.External, Internal and None
.
C.External and None D.Internal
B

Answer: Option B

Explanation:
External Linkage-> means global, non-static variables and functions.
Internal Linkage-> means static variables and functions with file scope.
None Linkage-> means Local variables.

3.  Which of the following special symbol allowed in a variable?


A
* (asterisk) B.| (pipeline)
.
C.- (hyphen) D._ (underscore)
D

Answer: Option D

Explanation:

Variable names in C are made up of letters (upper and lower case) and digits. The underscore
character ("_") is also permitted. Names must not begin with a digit.

Examples of valid (but not very descriptive) C variable names:


=> foo
=> Bar
=> BAZ
=> foo_bar
=> _foo42
=> _
=> QuUx

4.  Is there any difference between following declarations?


1 : extern int fun();
2 : int fun();
A
Both are identical
.
B.No difference, except extern int fun(); is probably in another file
C.int fun(); is overrided with extern int fun();
D
None of these
.
B

Answer: Option B

Explanation:

extern int fun(); declaration in C is to indicate the existence of a global function and it is defined
externally to the current module or in another file.
int fun(); declaration in C is to indicate the existence of a function inside the current module or in
the same file.

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

Answer: Option A

Explanation:

/* Example for ceil() and floor() functions: */

#include<stdio.h>
#include<math.h>

int main()
{
printf("\n Result : %f" , ceil(1.66) );

printf("\n Result : %f" , floor(1.66) );

return 0;
}

// Output: Result : 2.000000


// Output: Result : 1.000000

6.  By default a real number is treated as a


A
float B.double
.
C.long double D.far double
B

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?


struct book
{
char name[10];
1: float price;
int pages;
};
2: long int l = 2.35;
3 : enum day {Sun, Mon, Tue, Wed};
A
1 B.2
.
C.3 D.Both 1 and 2
B

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

8.  Is the following statement a declaration or definition?


extern int i;
A
Declaration B.Definition
.
C.Function D.Error
A
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
C

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 definition
C.int a = 20 is definition, a is not defined
D
a is declared, a is not defined
.
A

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
Defining B.Declaring
.
C.Prototyping D.Calling
B

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 Declarations and Initializations Find Output of Program


1.  What is the output of the program
#include<stdio.h>
int main()
{
enum status { pass, fail, atkt};
enum status stud1, stud2, stud3;
stud1 = pass;
stud2 = atkt;
stud3 = fail;
printf("%d, %d, %d\n", stud1, stud2, stud3);
return 0;
}
A
0, 1, 2 B.1, 2, 3
.
C.0, 2, 1 D.1, 3, 2
C

Answer: Option C
Explanation:

enum takes the format like {0,1,2..) so pass=0, fail=1, atkt=2

stud1 = pass (value is 0)

stud2 = atkt (value is 2)

stud3 = fail (value is 1)

Hence it prints 0, 2, 1

2.  What will be the output of the program?


#include<stdio.h>
int main()
{
extern int i;
i = 20;
printf("%d\n", sizeof(i));
return 0;
}
A
2
.
B.4
C.vary from compiler
D
Linker Error : Undefined symbol 'i'
.
D

Answer: Option D

Explanation:

Linker Error : Undefined symbol 'i'


The statement extern int i specifies to the compiler that the memory for 'i' is allocated in some
other program and that address will be given to the current program at the time of linking. But
linker finds that no other variable of name 'i' is available in any other program with memory
space allocated for it. Hence a linker error has occurred.

3.  What is the output of the program?


#include<stdio.h>
int main()
{
extern int a;
printf("%d\n", a);
return 0;
}
int a=20;
A 20 B.0
.
C.Garbage Value D.Error
A

Answer: Option A

Explanation:

extern int a; indicates that the variable a is defined elsewhere, usually in a separate source code
module.

printf("%d\n", a); it prints the value of local variable int a = 20. Because, whenever there is a
conflict between local variable and global variable, local variable gets the highest priority. So it
prints 20.

4.  What is the output of the program


#include<stdio.h>
int main()
{
char *s1;
char far *s2;
char huge *s3;
printf("%d, %d, %d\n", sizeof(s1), sizeof(s2), sizeof(s3));
return 0;
}
A
2, 4, 6 B.4, 4, 2
.
C.2, 4, 4 D.2, 2, 2
C

Answer: Option C

Explanation:

Any pointer size is 2 bytes. (only 16-bit offset)


So, char *s1 = 2 bytes.
So, char far *s2; = 4 bytes.
So, char huge *s3; = 4 bytes.
A far, huge pointer has two parts: a 16-bit segment value and a 16-bit offset value.

Since C is a compiler dependent language, it may give different output in ohter platforms. The
above program works fine in Windows (TurboC), but error in Linux (GCC Compiler).

5.  What is the output of the program


#include<stdio.h>
int main()
{
struct emp
{
char name[20];
int age;
float sal;
};
struct emp e = {"Tiger"};
printf("%d, %f\n", e.age, e.sal);
return 0;
}
A
0, 0.000000 B.Garbage values
.
C.Error D.None of above
A

Answer: Option A

Explanation:

When an automatic structure is partially initialized remaining elements are initialized to 0(zero).

6.  What will be the output of the program?


#include<stdio.h>
int X=40;
int main()
{
int X=20;
printf("%d\n", X);
return 0;
}
A
20 B.40
.
C.Error D.No Output
A

Answer: Option A

Explanation:

Whenever there is conflict between a local variable and global variable, the local variable gets
priority.

7.  What is the output of the program


#include<stdio.h>
int main()
{
int x = 10, y = 20, z = 5, i;
i = x < y < z;
printf("%d\n", i);
return 0;
}
A
0 B.1
.
C.Error D.None of these
B

Answer: Option B

Explanation:

Since x < y turns to be TRUE it is replaced by 1. Then 1 < z is compared and to be TRUE. The 1
is assigned to i.

8.  What is the output of the program


#include<stdio.h>
int main()
{
extern int fun(float);
int a;
a = fun(3.14);
printf("%d\n", a);
return 0;
}
int fun(aa)
float aa;
{
return ((int)aa);
}
A
3 B.3.14
.
C.0 D.Error
D

Answer: Option D

Explanation:

2 Errors
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa

9.  What is the output of the program


#include<stdio.h>
int main()
{
int a[5] = {2, 3};
printf("%d, %d, %d\n", a[2], a[3], a[4]);
return 0;
}
A Garbage Values B.2, 3, 3
.
C.3, 2, 2 D.0, 0, 0
D

Answer: Option D

Explanation:

When an automatic array is partially initialized, the remaining elements are initialized to 0.

10.  What is the output of the program


#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
A
3, 2, 515 B.515, 2, 3
.
C.3, 2, 5 D.None of these
A

Answer: Option A

Explanation:

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); It prints the value of u.ch[0] = 3, u.ch[1] = 2 and
it prints the value of u.i means the value of entire union size.
So the output is 3, 2, 515.

11.  In the following program how long will the for loop get executed?
#include<stdio.h>
int main()
{
int i;
for(;scanf("%s", &i); printf("%d\n", i));
return 0;
}
A
The for loop would not get executed at all
.
B.The for loop would get executed only once
C.The for loop would get executed 5 times
D
The for loop would get executed infinite times
.
D

Answer: Option D

Explanation:

During the for loop execution scanf() ask input and then printf() prints that given input. This
process will be continued repeatedly because, scanf() returns the number of input given, the
condition is always true(user gives a input means it reurns '1').

Hence this for loop would get executed infinite times.

12.  What will be the output of the program?


#include<stdio.h>
int main()
{
int X=40;
{
int X=20;
printf("%d ", X);
}
printf("%d\n", X);
return 0;
}
A
40 40 B.20 40
.
C.20 D.Error
B

Answer: Option B

Explanation:

In case of a conflict between a local variable and global variable, the local variable gets priority.

C Programming > Declarations and Initializations > Point Out Errors


1.  Point out the error in the following program.
#include<stdio.h>
int main()
{
display();
return 0;
}
void display()
{
printf("IndiaBIX.com");
}
A
No error
.
B.display() doesn't get invoked
C.display() is called before it is defined
D
None of these
.
C

Answer: Option C

Explanation:

In this program the compiler will not know that the function display() exists. So, the compiler
will generate "Type mismatch in redeclaration of function display()".

To over come this error, we have to add function prototype of function display().
Another way to overcome this error is to define the function display() before the int main();
function.
#include<stdio.h>
void display(); /* function prototype */

int main()
{
display();
return 0;
}
void display()
{
printf("IndiaBIX.com");
}

Output: IndiaBIX.com

Note: This problem will not occur in modern compilers (this problem occurs in TurboC but not
in GCC).

2.  Point out the error in the following program.


#include<stdio.h>
int main()
{
void v = 0;

printf("%d", v);

return 0;
}
A
Error: Declaration syntax error 'v' (or) Size of v is unknown or zero.
.
B.Program terminates abnormally.
C.No error.
D
None of these.
.
A

Answer: Option A

3.  Point out the error in the following program.


#include<stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
emp int xx;
int a;
printf("%d\n", &a);
return 0;
}
A
Error: in printf B.Error: in emp int xx;
.
C.No error. D.None of these.
B

Answer: Option B

Explanation:

There is an error in the line emp int xx;

To overcome this error, remove the int and add the struct at the begining of emp int xx;

#include<stdio.h>
struct emp
{
char name[20];
int age;
};
int main()
{
struct emp xx;
int a;
printf("%d\n", &a);
return 0;
}

4.  Which of the following is correct about err used in the declaration given below?
typedef enum error { warning, test, exception } err;
A
It is a typedef for enum error.
.
B.It is a variable of type enum error.
C.The statement is erroneous.
D
It is a structure.
.
A

Answer: Option A

Explanation:

A typedef gives a new name to an existing data type.


So err is a new name for enum error.

5.  Point out the error in the following program.


#include<stdio.h>
int main()
{
int (*p)() = fun;
(*p)();
return 0;
}
int fun()
{
printf("IndiaBix.com\n");
return 0;
}
A
Error: in int(*p)() = fun;
.
B.Error: fun() prototype not defined
C.No error
D
None of these
.
B

Answer: Option B

Explanation:

The compiler will not know that the function int fun() exists. So we have to define the function
prototype of int fun();
To overcome this error, see the below program

#include<stdio.h>
int fun(); /* function prototype */

int main()
{
int (*p)() = fun;
(*p)();
return 0;
}
int fun()
{
printf("IndiaBix.com\n");
return 0;
}

C Programming > Declarations and Initializations > Point Out Correct Statements

1.  Which of the definition is correct?


A
int length; B.char int;
.
C.int long; D.float double;
A
Answer: Option A

Explanation:

int length; denotes that varaible length is int(integer) data type.

char int; here int is a keyword cannot be used a variable name.

int long; here long is a keyword cannot be used a variable name.

float double; here double is a keyword cannot be used a variable name.

So, the answer is int length;(Option A).

2.  Which of the following operations are INCORRECT?


A int i = 35; i = i%5;
B. short int j = 255; j = j;
.
C. long int k = 365L; k = k; D.float a = 3.14; a = a%3;
D

Answer: Option D

Explanation:

float a = 3.14; a = a%3; gives "Illegal use of floating point" error.

The modulus (%) operator can only be used on integer types. We have to use fmod() function in
math.h for float values.

3.  Which of the following correctly represents a long double constant?


A
6.68 B.6.68L
.
C.6.68f D.6.68LF
B

Answer: Option B

Explanation:

6.68 is double.
6.68L is long double constant.
6.68f is float constant.
6.68LF is not allowed in c.

4.  Which of the structure is incorrcet?


1 : struct aa
{
int a;
float b;
};
struct aa
{
int a;
2: float b;
struct aa var;
};
struct aa
{
int a;
3: float b;
struct aa *var;
};
A
1 B.2
.
C.3 D.1, 2, 3
B

Answer: Option B

Explanation:

Option B gives "Undefined structure in 'aa'" error.

5.  Which of the structure is corrcet?


struct book
{
char name[10];
1: float price;
int pages;
};
struct aa
{
char name[10];
2: float price;
int pages;
}
struct aa
{
char name[10];
3: float price;
int pages;
}
A
1 B.2
.
C.3 D.All of above
A
Answer: Option A

Explanation:

In 2 and 3 semicolon are missing in structure element.

typedef long a;
6.  1 :
extern int a c;
typedef long a;
2:
extern a int c;
typedef long a;
3:
extern a c;
A
1 correct B.2 correct
.
C.3 correct D.1, 2, 3 are correct
C

Answer: Option C

Explanation:

typedef long a;
extern int a c; while compiling this statement becomes extern int long c;. This will result in to
"Declaration syntax error".

typedef long a;
extern a int c; while compiling this statement becomes extern long int c;. This will result in to
"Too many types in declaration error".

typedef long a;
extern a c; while compiling this statement becomes extern long c;. This is a valid c declaration
statement. It says variable c is long data type and defined in some other file or module.

So, Option C is the correct answer.

C Programming > Floating Point Issues > General Questions

1.  What are the different types of real data type in C ?


A
float, double B.short int, double, long int
.
C.float, double, long double D.double, long int, float
C
Answer: Option C

Explanation:

The floating point data types are called real data types. Hence float, double, and long double are
real data types.

2.  What will you do to treat the constant 3.14 as a long double?
A
use 3.14LD B.use 3.14L
.
C.use 3.14DL D.use 3.14LF
B

Answer: Option B

Explanation:

Given 3.14 is a double constant.

To specify 3.14 as long double, we have to add L to the 3.14. (i.e 3.14L)

3.  If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000
0000, what will be the output of the program (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
40 AC 00 00 B.04 CA 00 00
.
C.00 00 AC 40 D.00 00 CA 04
C

Answer: Option C

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
A

Answer & Explanation


Answer: Option A

Explanation:

The range of long double is 3.4E-4932 to 1.1E+4932

5.  Which statement will you add in the following program to work it correctly?
#include<stdio.h>
int main()
{
printf("%f\n", log(36.0));
return 0;
}
A
#include<conio.h> B.#include<math.h>
.
C.#include<stdlib.h> D.#include<dos.h>
B

Answer: Option B

Explanation:

math.h is a header file in the standard library of C programming language designed for basic
mathematical operations.

Declaration syntax: double log(double);

6.  We want to round off x, a float, to an int value, The correct way to do is
A
y = (int)(x + 0.5) B.y = int(x + 0.5)
.
C.y = (int)x + 0.5 D.y = (int)((int)x + 0.5)
A

Answer: 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)

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.

7.  The binary equivalent of 5.375 is


A
101.101110111 B.101.011
.
C.101011 D.None of above
B

Answer: Option B

8.  A float occupies 4 bytes. If the hexadecimal equivalent of these 4 bytes are A, B, C and D,
then when this float is stored in memory in which of the following order do these bytes gets
stored?
A
ABCD
.
B.DCBA
C.0xABCD
D
Depends on big endian or little endian architecture
.
D

Answer: Option D

9.  What will you do to treat the constant 3.14 as a float?


A
use float(3.14f) B.use 3.14f
.
C.use f(3.14) D.use (f)(3.14)
B

Answer: Option B

10.  Which of the following statement obtains the remainder on dividing 5.5 by 1.3 ?
A
rem = (5.5 % 1.3) B.rem = modf(5.5, 1.3)
.
C.rem = fmod(5.5, 1.3) D.Error: we can't divide
C
Answer: 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

C Programming > Floating Point Issues > Find Output of Program

1.  What will be the output of the program?


#include<stdio.h>
int main()
{
float a=0.7;
if(a < 0.7)
printf("C\n");
else
printf("C++\n");
return 0;
}
A
C B.C++
.
C.Compiler error D.Non of above
A

Answer: Option A

Explanation:

if(a < 0.7) here a is a float variable and 0.7 is a double constant. The float variable a is less than
double constant 0.7. Hence the if condition is satisfied and it prints 'C'
Example:

#include<stdio.h>
int main()
{
float a=0.7;
printf("%.10f %.10f\n",0.7, a);
return 0;
}

Output:
0.7000000000 0.6999999881

2.  What will be the output of the program?


#include<stdio.h>
int main()
{
float *p;
printf("%d\n", sizeof(p));
return 0;
}
A
2 in 16bit compiler, 4 in 32bit compiler
.
B.4 in 16bit compiler, 2 in 32bit compiler
C.4 in 16bit compiler, 4 in 32bit compiler
D
2 in 16bit compiler, 2 in 32bit compiler
.
A

Answer: Option A

Explanation:

sizeof(x) returns the size of x in bytes.


float *p is a pointer to a float.

In 16 bit compiler, the pointer size is always 2 bytes.


In 32 bit compiler, the pointer size is always 4 bytes.

3.  What will be the output of the program?


#include<stdio.h>
int main()
{
float fval=7.29;
printf("%d\n", (int)fval);
return 0;
}
A
0 B.0.0
.
C.7.0 D.7
D

Answer: Option D
Explanation:

printf("%d\n", (int)fval); It prints '7'. because, we typecast the (int)fval in to integer. It converts
the float value to the nearest integer value.

4.  What will be the output of the program?


#include<stdio.h>
#include<math.h>
int main()
{
printf("%f\n", sqrt(36.0));
return 0;
}
A
6.0 B.6
.
C.6.000000 D.Error: Prototype sqrt() not found.
C

Answer: Option C

Explanation:

printf("%f\n", sqrt(36.0)); It prints the square root of 36 in the float format(i.e 6.000000).

Declaration Syntax: double sqrt(double x) calculates and return the positive square root of the
given number.

5.  What will be the output of the program?


#include<stdio.h>
#include<math.h>
int main()
{
printf("%d, %d, %d\n", sizeof(3.14f), sizeof(3.14), sizeof(3.14l));
return 0;
}
A
4, 4, 4 B.4, 8, 8
.
C.4, 8, 10 D.4, 8, 12
C

Answer: Option C

Explanation:

sizeof(3.14f) here '3.14f' specifies the float data type. Hence size of float is 4 bytes.

sizeof(3.14) here '3.14' specifies the double data type. Hence size of float is 8 bytes.
sizeof(3.14l) here '3.14l' specifies the long double data type. Hence size of float is 10 bytes.

Note: If you run the above program in Linux platform (GCC Compiler) it will give 4, 8, 12 as
output. If you run in Windows platform (TurboC Compiler) it will give 4, 8, 10 as output.
Because, C is a machine dependent language.

6.  What will be the output of the program?


#include<stdio.h>
int main()
{
float f=43.20;
printf("%e, ", f);
printf("%f, ", f);
printf("%g", f);
return 0;
}
A
4.320000e+01, 43.200001, 43.2 B.4.3, 43.22, 43.21
.
C.4.3e, 43.20f, 43.00 D.Error
A

Answer: Option A

Explanation:

printf("%e, ", f); Here '%e' specifies the "Scientific Notation" format. So, it prints the 43.20 as
4.320000e+01.

printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 43.20
as 43.200001.

printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2.

7.  What will be the output of the program?


#include<stdio.h>
int main()
{
float a=0.7;
if(a < 0.7f)
printf("C\n");
else
printf("C++\n");
return 0;
}
A
C B.C++
.
C.Compiler error D.Non of above
B
Answer: Option B

Explanation:

if(a < 0.7f) here a is a float variable and 0.7f is a float constant. The float variable a is not less
than 0.7f float constant. But both are equal. Hence the if condition is failed and it goes to else it
prints 'C++'
Example:

#include<stdio.h>
int main()
{
float a=0.7;
printf("%.10f %.10f\n",0.7f, a);
return 0;
}

Output:
0.6999999881 0.6999999881

8.  What will be the output of the program?


#include<stdio.h>
#include<math.h>
int main()
{
float n=1.54;
printf("%f, %f\n", ceil(n), floor(n));
return 0;
}
A
2.000000, 1.000000 B.1.500000, 1.500000
.
C.1.550000, 2.000000 D.1.000000, 2.000000
A

Answer: Option A

Explanation:

ceil(x) round up the given value. It finds the smallest integer not < x.
floor(x) round down the given value. It finds the smallest integer not > x.

printf("%f, %f\n", ceil(n), floor(n)); In this line ceil(1.54) round up the 1.54 to 2 and floor(1.54)
round down the 1.54 to 1.

In the printf("%f, %f\n", ceil(n), floor(n)); statement, the format specifier "%f %f" tells output to
be float value. Hence it prints 2.000000 and 1.000000.

9.  What will be the output of the program?


#include<stdio.h>
int main()
{
float d=2.25;
printf("%e,", d);
printf("%f,", d);
printf("%g,", d);
printf("%lf", d);
return 0;
}
A
2.2, 2.50, 2.50, 2.5
.
B.2.2e, 2.25f, 2.00, 2.25
C.2.250000e+000, 2.250000, 2.25, 2.250000
D
Error
.
C

Answer: Option C

Explanation:

printf("%e,", d); Here '%e' specifies the "Scientific Notation" format. So, it prints the 2.25 as
2.250000e+000.

printf("%f,", d); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 2.25 as
2.250000.

printf("%g,", d); Here '%g' "Use the shorter of %e or %f". So, it prints the 2.25 as 2.25.

printf("%lf,", d); Here '%lf' specifies the "Long Double" format. So, it prints the 2.25 as
2.250000.

C Programming > Pointers > General Questions

1.  What is (void*)0?


A
Representation of NULL pointer
.
B.Representation of void pointer
C.Error
D
None of above
.
A

Answer: Option A

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.char *p = (char*)malloc(100);
D
char *p = (char *)(malloc*)(100);
.
C

Answer: Option C

3.  In which header file is the NULL macro defined?


A
stdio.h B.stddef.h
.
C.stdio.h and stddef.h D.stdlib.h
C

Answer: Option C

4.  How many bytes are occupied by near, far and huge pointers (DOS)?
A
near=2 far=4 huge=4 B.near=4 far=8 huge=8
.
C.near=2 far=4 huge=8 D.near=4 far=8 huge=8
A

Answer: Option A

5.  If a variable is a pointer to a structure, then which of the following operator is used to access
data members of the structure through the pointer variable?
A
'.' B.'&'
.
C.'*' D.'->'
D

Answer: Option D

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

Answer: Option B

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

Answer: Option C

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

Answer: Option A

C Programming > Pointers > Find Output of Program

1.  What will be the output of the program ?


#include<stdio.h>

int main()
{
static char *s[] = {"black", "white", "pink", "violet"};
char **ptr[] = {s+3, s+2, s+1, s}, ***p;
p = ptr;
++p;
printf("%s", **p+1);
return 0;
}
A
ink B.ack
.
C.ite D.let
A

Answer: Option A

2.  What will be the output of the program ?


#include<stdio.h>

int main()
{
int i=3, *j, k;
j = &i;
printf("%d\n", i**j*i+*j);
return 0;
}
A 30 B.27
.
C.9 D.3
A

Answer: Option A

3. What will be the output of the program ?


#include<stdio.h>

int main()
{
int x=30, *y, *z;
y=&x; /* Assume address of x is 500 and integer is 4 byte size */
z=y;
*y++=*z++;
x++;
printf("x=%d, y=%d, z=%d\n", x, y, z);
return 0;
}
A
x=31, y=502, z=502 B.x=31, y=500, z=500
.
C.x=31, y=498, z=498 D.x=31, y=504, z=504
D

Answer: Option D

4.  What will be the output of the program ?


#include<stdio.h>

int main()
{
char str[20] = "Hello";
char *const p=str;
*p='M';
printf("%s\n", str);
return 0;
}
A
Mello B.Hello
.
C.HMello D.MHello
A

Answer: Option A

5.  What will be the output of the program If the integer is 4bytes long?
#include<stdio.h>

int main()
{
int ***r, **q, *p, i=8;
p = &i;
q = &p;
r = &q;
printf("%d, %d, %d\n", *p, **q, ***r);
return 0;
}
A
8, 8, 8 B.4000, 4002, 4004
.
C.4000, 4004, 4008 D.4000, 4008, 4016
A

Answer: Option A

6.  What will be the output of the program ?


#include<stdio.h>

void fun(void *p);


int i;

int main()
{
void *vptr;
vptr = &i;
fun(vptr);
return 0;
}
void fun(void *p)
{
int **q;
q = (int**)&p;
printf("%d\n", **q);
}
A
Error: cannot convert from void** to int**
.
B.Garbage value
C.0
D
No output
.
C

Answer: Option C

7.  What will be the output of the program ?


#include<stdio.h>

int main()
{
char *str;
str = "%s";
printf(str, "K\n");
return 0;
}
A
Error B.No output
.
C.K D.%s
C

Answer: Option C

8.  What will be the output of the program ?


#include<stdio.h>
int *check(static int, static int);

int main()
{
int *c;
c = check(10, 20);
printf("%d\n", c);
return 0;
}
int *check(static int i, static int j)
{
int *p, *q;
p = &i;
q = &j;
if(i >= 45)
return (p);
else
return (q);
}
A
10
.
B.20
C.Error: Non portable pointer conversion
D
Error: cannot use static for function parameters
.
D

Answer: Option D

9.  What will be the output of the program if the size of pointer is 4-bytes?
#include<stdio.h>

int main()
{
printf("%d, %d\n", sizeof(NULL), sizeof(""));
return 0;
}
A
2, 1 B.2, 2
.
C.4, 1 D.4, 2
C

Answer: Option C

Explanation:

In TurboC, the output will be 2, 1 because the size of the pointer is 2 bytes in 16-bit platform.

But in Linux, the output will be 4, 1 because the size of the pointer is 4 bytes.

This difference is due to the platform dependency of C compiler.

10.  What will be the output of the program ?


#include<stdio.h>

int main()
{
void *vp;
char ch=74, *cp="JACK";
int j=65;
vp=&ch;
printf("%c", *(char*)vp);
vp=&j;
printf("%c", *(int*)vp);
vp=cp;
printf("%s", (char*)vp+2);
return 0;
}
A
JCK B.J65K
.
C.JAK D.JACK
D

Answer: Option D

11.  What will be the output of the program?


#include<stdio.h>

int main()
{
int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
int *p, *q;
p = &arr[1][1][1];
q = (int*) arr;
printf("%d, %d\n", *p, *q);
return 0;
}
A
8, 10 B.10, 2
.
C.8, 1 D.Garbage values
A

Answer: Option A

12.  What will be the output of the program assuming that the array begins at the location 1002
and size of an integer is 4 bytes?
#include<stdio.h>

int main()
{
int a[3][4] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
printf("%u, %u, %u\n", a[0]+1, *(a[0]+1), *(*(a+0)+1));
return 0;
}
A
448, 4, 4 B.520, 2, 2
.
C.1006, 2, 2 D.Error
C

Answer: Option C

13.  What will be the output of the program?


#include<stdio.h>

int main()
{
int arr[3] = {2, 3, 4};
char *p;
p = arr;
p = (char*)((int*)(p));
printf("%d, ", *p);
p = (int*)(p+1);
printf("%d", *p);
return 0;
}
A
2, 3 B.2, 0
.
C.2, Garbage value D.0, 0
B

Answer: Option B

14.  What will be the output of the program ?


#include<stdio.h>

int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
return 0;
}
A
No output B.30
.
C.3 D.300
D

Answer: Option D

15.  What will be the output of the program ?


#include<stdio.h>

int main()
{
printf("%c\n", 7["IndiaBIX"]);
return 0;
}
A
Error: in printf B.Nothing will print
.
C.print "X" of IndiaBIX D.print "7"
C

Answer: Option C

16.  What will be the output of the program ?


#include<stdio.h>

int main()
{
char str[] = "peace";
char *s = str;
printf("%s\n", s++ +3);
return 0;
}
A
peace B.eace
.
C.ace D.ce
D

Answer: Option D

17.  What will be the output of the program ?


#include<stdio.h>

int main()
{
char *p;
p="hello";
printf("%s\n", *&*&p);
return 0;
}
A
llo B.hello
.
C.ello D.h
B

Answer: Option B

18.  What will be the output of the program assuming that the array begins at location 1002?
#include<stdio.h>

int main()
{
int a[2][3][4] = { {1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2},
{2, 1, 4, 7, 6, 7, 8, 9, 0, 0, 0, 0} };
printf("%u, %u, %u, %d\n", a, *a, **a, ***a);
return 0;
}
A
1002, 2004, 4008, 2 B.2004, 4008, 8016, 1
.
C.1002, 1002, 1002, 1 D.Error
C

Answer: Option C

19.  What will be the output of the program ?


#include<stdio.h>
power(int**);
int main()
{
int a=5, *aa; /* Address od 'a' is 1000 */
aa = &a;
a = power(&aa);
printf("%d\n", a);
return 0;
}
power(int **ptr)
{
int b;
b = **ptr***ptr;
return (b);
}
A
5 B.25
.
C.125 D.Garbage value
B

Answer: Option B
20.  What will be the output of the program ?
#include<stdio.h>

int main()
{
char str1[] = "India";
char str2[] = "BIX";
char *s1 = str1, *s2=str2;
while(*s1++ = *s2++)
printf("%s", str1);

printf("\n");
return 0;
}
A
IndiaBIX B.BndiaBIdiaBIXia
.
C.India D.(null)
B

Answer: Option B

21.  What will be the output of the program ?


#include<stdio.h>
#include<string.h>

int main()
{
int i, n;
char *x="Alice";
n = strlen(x);
*x = x[n];
for(i=0; i<=n; i++)
{
printf("%s ", x);
x++;
}
printf("\n", x);
return 0;
}
A
Alice B.ecilA
.
C.Alice lice ice ce e D.lice ice ce e
D

Answer: Option D

Explanation:

If you compile and execute this program in windows platform with Turbo C, it will give "lice ice
ce e".
It may give different output in other platforms (depends upon compiler and machine). The online
C compiler given in this site will give the Option C as output (it runs on Linux platform).

22.  What will be the output of the program ?


#include<stdio.h>

int main()
{
int i, a[] = {2, 4, 6, 8, 10};
change(a, 5);
for(i=0; i<=4; i++)
printf("%d, ", a[i]);
return 0;
}
change(int *b, int n)
{
int i;
for(i=0; i<n; i++)
*(b+1) = *(b+i)+5;
}
A
7, 9, 11, 13, 15 B.2, 15, 6, 8, 10
.
C.2 4 6 8 10 D.3, 1, -1, -3, -5
B

Answer: Option B

23.  If the size of integer is 4bytes. What will be the output of the program?
#include<stdio.h>

int main()
{
int arr[] = {12, 13, 14, 15, 16};
printf("%d, %d, %d\n", sizeof(arr), sizeof(*arr), sizeof(arr[0]));
return 0;
}
A
10, 2, 4 B.20, 4, 4
.
C.16, 2, 2 D.20, 2, 2
B

Answer: Option B

C Programming Pointers - Point Out Errors


1.  Point out the error in the program
#include<stdio.h>
int main()
{
int *x;
*x=100;
return 0;
}
A
Error: invalid assignment for x
.
B.Error: suspicious pointer conversion
C.No error
D
None of above
.
C

Answer: Option C

While reading the code there is no error, but upon running the program having an unitialised variable
can cause the program to crash (Null pointer assignment).

2.  Point out the error in the program


#include<stdio.h>

int main()
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a);
a++;
}
return 0;
}
A
Error: Declaration syntax B.Error: Expression syntax
.
C.Error: LValue required D.Error: Rvalue required
C

Answer: Option C

C Programming > Pointers > Point Out Correct Statements

1.  Which of the following statements correctly declare a function that receives a pointer to
pointer to a pointer to a float and returns a pointer to a pointer to a pointer to a pointer to a float?
A
float **fun(float***); B.float *fun(float**);
.
C.float fun(float***); D.float ****fun(float***);
D
Answer: Option D

2.  Which of the statements is correct about the program?


#include<stdio.h>

int main()
{
int i=10;
int *j=&i;
return 0;
}
A
j and i are pointers to an int
.
B.i is a pointer to an int and stores address of j
C.j is a pointer to an int and stores address of i
D
j is a pointer to a pointer to an int and stores address of i
.
C

Answer: Option C

3.  Which of the statements is correct about the program?


#include<stdio.h>

int main()
{
float a=3.14;
char *j;
j = (char*)&a;
printf("%d\n", *j);
return 0;
}
A
It prints ASCII value of the binary number present in the first byte of a float variable a.
.
B.It prints character equivalent of the binary number present in the first byte of a float variable a.
C.It will print 3
D
It will print a garbage value
.
A

Answer: Option A

4.  In the following program add a statement in the function fun() such that address of a gets
stored in j?
#include<stdio.h>
int main()
{
int *j;
void fun(int**);
fun(&j);
return 0;
}
void fun(int **k)
{
int a=10;
/* Add a statement here */
}
A
**k=a; B.k=&a;
.
C.*k=&a D.&k=*a
C

Answer: Option C

5.  Which of the following statements correct about k used in the below statement?
char ****k;
A
k is a pointer to a pointer to a char
.
B.k is a pointer to a pointer to a pointer to a char
C.k is a pointer to a char pointer
D
k is a pointer to a pointer to a char
.
B

Answer: Option B

You might also like