Sunday, 23 August 2015

geek-test--structor

                                           

Macro & Preprocessor



uestion 1
WRONG
#include <stdio.h>
#define PRINT(i, limit) do \
                        { \
                            if (i++ < limit) \
                            { \
                                printf("GeeksQuiz\n"); \
                                continue; \
                            } \
                        }while(1)
 
int main()
{
    PRINT(0, 3);
    return 0;
}
How many times GeeksQuiz is printed in the above program?
1
B
3
C
4
Compile-time error

Discuss it


Question 1 Explanation: 
The PRINT macro gets expanded at the pre-processor time i.e. before the compilation time. After the macro expansion, the if expression becomes: if (0++ < 3). Since 0 is a constant figure and represents only r-value, applying increment operator gives compile-time error: lvalue required. lvalue means a memory location with some address.



Assume that size of an integer is 32 bit. What is the output of following program?
#include<stdio.h>
struct st
{
    int x;
    static int y;
};
 
int main()
{
    printf("%d", sizeof(struct st));
    return 0;
}
A
4
B
8
Compiler Error
D
Runtime Error

Discuss it


Question 2 Explanation: 
In C, struct and union types cannot have static members. In C++, struct types are allowed to have static members, but union cannot have static members in C++ also.



Question 10
WRONG
Predict the output of following C program
#include<stdio.h>
struct Point
{
  int x, y, z;
};
 
int main()
{
  struct Point p1 = {.y = 0, .z = 1, .x = 2};
  printf("%d %d %d", p1.x, p1.y, p1.z);
  return 0;
}
Compiler Error
2 0 1
C
0 1 2
D
2 1 0

Discuss it


Question 10 Explanation: 
Refer designated Initialization discussed here.


Question 11
CORRECT
What is the use of "#pragma once"?
Used in a header file to avoid its inclusion more than once.
B
Used to avoid multiple declarations of same variable.
C
Used in a c file to include a header file at least once.
D
Used to avoid assertions

Question 10
WRONG
Which file is generated after pre-processing of a C program?
A
.p
.i
.o
D
.m

Discuss it


Question 10 Explanation: 
After the pre-processing of a C program, a .i file is generated which is passed to the compiler for compilation.
Question 9
CORRECT
# include <iostream>
# include <string.h>
using namespace std;
 
struct Test
{
  char str[20];
};
 
int main()
{
  struct Test st1, st2;
  strcpy(st1.str, "GeeksQuiz");
  st2 = st1;
  st1.str[0] = 'S';
  cout << st2.str;
  return 0;
}
A
Segmentation Fault
B
SeeksQuiz
GeeksQuiz
D
Compiler Error

Discuss it


Question 9 Explanation: 
Array members are deeply copied when a struct variable is assigned to another one. SeeAre array members deeply copied? for more details.
uestion 5
WRONG
#include<stdio.h>
struct st
{
    int x;
    struct st next;
};
   
int main()
{
    struct st temp;
    temp.x = 10;
    temp.next = temp;
    printf("%d", temp.next.x);
    return 0;
}
Compiler Error
10
C
Runtime Error
D
Garbage Value

Discuss it


Question 5 Explanation: 
A structure cannot contain a member of its own type because if this is allowed then it becomes impossible for compiler to know size of such struct. Although a pointer of same type can be a member because pointers of all types are of same size and compiler can calculate size of struct



Question 6
CORRECT
Which of the following operators can be applied on structure variables?
A
Equality comparison ( == )
Assignment ( = )
C
Both of the above
D
None of the above

Discuss it


Question 6 Explanation: 
A structure variable can be assigned to other using =, but cannot be compared with other using ==

Question 7
CORRECT
union test
{
    int x;
    char arr[8];
    int y;
};
 
int main()
{
    printf("%d", sizeof(union test));
    return 0;
}
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
A
12
B
16
8
D
Compiler Error

Discuss it


Question 7 Explanation: 
When we declare a union, memory allocated for a union variable of the type is equal to memory needed for the largest member of it, and all members share this same memory space. In above example, "char arr[8]" is the largest member. Therefore size of union test is 8 bytes.
union test
{
    int x;
    char arr[4];
    int y;
};
 
int main()
{
    union test t;
    t.x = 0;
    t.arr[1] = 'G';
    printf("%s", t.arr);
    return 0;
}
Predict the output of above program. Assume that the size of an integer is 4 bytes and size of character is 1 byte. Also assume that there is no alignment needed.
Nothing is printed
G
C
Garbage character followed by 'G'
D
Garbage character followed by 'G', followed by more garbage characters
E
Compiler Error


Question 1
WRONG
Which of the following is not a storage class specifier in C?
A
auto
B
register
C
static
D
extern
volatile
typedef

Discuss it


Question 1 Explanation: 
volatile is not a storage class specifier. volatile and const are type qualifiers.


jsadjashdas

No comments:

Post a Comment