Friday, 17 July 2015

declaration

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

ans-c

fmod(x,y) - Calculates x modulo y, the remainder of x/y. 
This function is the same as the modulus operator. But fmod() performs floating point divisions.





2-- 
What are the types of linkages?
A.Internal and ExternalB.External, Internal and None
C.External and NoneD.Internal
ans-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--
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 thes
ans--
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.




4--
By default a real number is treated as a
A.floatB.double
C.long doubleD.far double
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 doubleto 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.



5--
Identify which of the following are declarations
1 :extern int x;
2 :float square ( float x ) { ... }
3 :double pow(double, double);
A.1B.2
C.1 and 3D.3
ANS--
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.



Q--6

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
Answer & Explanation
Answer: Option A
Explanation:
- During declaration we tell the datatype of the Variable.
- During definition the value is initialized.




Q--7
When we mention the prototype of a function?
A.DefiningB.Declaring
C.PrototypingD.Calling
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.



-------------------------------------------------------------------PREDCT OUTPUT ---------------------
1-What is the output of the program given below ?
#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, 2B.1, 2, 3
C.0, 2, 1D.1, 3, 2
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 in 16 bit platform (Turbo C under DOS)?
#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'

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.



Q3
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.20B.0
C.Garbage ValueD.Error

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.


 NOTE -- HERE a USE BY PRINTF IS THE  a DECLERED IN THE  THIS C CODE NOT a WHICH IS   LINKED BY  STATEMENT extern int a; WHICH EXIST IN SOME OTHER FILE ..




***Q4--

  predict the output ...

#include<iostream>

using namespace std;


int main()

 {

  int x,y,z;

  x=y=z=1;

    

   z=++x||++y&&++z;

     cout<<x<<y<<z<<endl;

     return 0;

 }

ans x=2,y=1,z=1;
 why --
 since ++x || ++y.. when it get ++x =2 which is positive , it stop further execution since if 1 statement is +ve than value of or does not depend on other value . so z=1 , x=2, y=1;
 
  but if it will be like 
 z=++x&&++y&&++z,
 since value of this line depends on all values sice && depend on all values so ans vakue of z will be  set after execution of entire line . s o  x=2,y=2 z=1;





Q5
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.000000B.Garbage values
C.ErrorD.None of above
Answer: Option A
Explanation:
When an automatic structure is partially initialized remaining elements are initialized to 0(zero).




Q6

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.0B.1
C.ErrorD.None of these
Answer: Option B
Explanation:
Since x < y turns to be TRUE it is replaced by 1. Then 1 < z is compared and to beTRUE. The 1 is assigned to i.



Q7
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(int aa)
{
    return (int)++aa;
}
A.3B.3.14
C.D.4
E.Compile Error
Answer: Option E
Explanation:
2 Errors
1. Type mismatch in redeclaration of fun
2. Type mismatch in parameter aa



q8--
#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 ValuesB.2, 3, 3
C.3, 2, 2D.0, 0, 0
Answer: Option D
Explanation:
When an automatic array is partially initialized, the remaining elements are initialized to 0.
4


Q10
 
In the following program how long will the for loop get executed?
#include<stdio.h>
int main()
{
    int i=5;
    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
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.

Q11
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;
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.

Q12
Which of the following correctly represents a long double constant?
A.6.68B.6.68L
C.6.68fD.6.68LF
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. 
Q13

6. 
1 :typedef long a;
extern int a c;
2 :typedef long a;
extern a int c;
3 :typedef long a;
extern a c;
A.1 correctB.2 correct
C.3 correctD.1, 2, 3 are correct
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.

Q
If the definition of the external variable occurs in the source file before its use in a particular function, then there is no need for an extern declaration in the function.
A.TrueB.False
Answer: Option A
Explanation:
True, When a function is declared inside the source file, that function(local function) get a priority than the extern function. So there is no need to declare a function asextern inside the same source file.


Q
Global variable are available to all functions. Does there exist a mechanism by way of which it available to some and not to others.
A.YesB.No
Answer: Option B
Explanation:
The only way this can be achieved is to define the variable locally in main() instead of defining it globally and then passing it to the functions which need it.


Q
Is it true that a global variable may have several declarations, but only one definition?
A.YesB.No
Answer: Option A
Explanation:
Yes, In all the global variable declarations, you need to use the keyword extern.


No comments:

Post a Comment