C Programming:: Declarations and Initializations: A - B. C. D
C Programming:: Declarations and Initializations: A - B. C. D
C Programming:: Declarations and Initializations: A - B. C. D
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:
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
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.
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.
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:
#include<stdio.h>
#include<math.h>
int main()
{
printf("\n Result : %f" , ceil(1.66) );
return 0;
}
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.
Answer: Option B
Explanation:
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".
Answer: Option C
Explanation:
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:
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.
Answer: Option C
Explanation:
Hence it prints 0, 2, 1
Answer: Option D
Explanation:
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.
Answer: Option C
Explanation:
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).
Answer: Option A
Explanation:
When an automatic structure is partially initialized remaining elements are initialized to 0(zero).
Answer: Option A
Explanation:
Whenever there is conflict between a local variable and global variable, the local variable gets
priority.
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.
Answer: Option D
Explanation:
2 Errors
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa
Answer: Option D
Explanation:
When an automatic array is partially initialized, the remaining elements are initialized to 0.
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').
Answer: Option B
Explanation:
In case of a conflict between a local variable and global variable, the local variable gets priority.
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).
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
Answer: Option B
Explanation:
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:
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
Explanation:
Answer: Option D
Explanation:
The modulus (%) operator can only be used on integer types. We have to use fmod() function in
math.h for float values.
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.
Answer: Option B
Explanation:
Explanation:
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.
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:
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
Explanation:
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.
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.
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
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:
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
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
Answer: Option A
Explanation:
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.
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.
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.
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.
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
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.
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.
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
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
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
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
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
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
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
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
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.
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
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
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
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
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
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
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
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
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).
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
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).
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
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
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
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