Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Tuesday, 18 March 2014

Test Your C Skills with Solution

1. 
How will you free the allocated memory ?
remove(var-name);
free(var-name);
delete(var-name);
dalloc(var-name);
 Answer: Option B

2. 
What is the similarity between a structure, union and enumeration?
A.
All of them let you define new values
B.
All of them let you define new data types
C.
All of them let you define new pointers
D.
All of them let you define new structures
Answer: Option B

3. What will be 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;
}
Answer: Option A
Explanation:
The system will allocate 2 bytes for the union.
The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.












4. #include<stdio.h>

int main()
{
    union var
    {
        int a, b;
    };
    union var v;
    v.a=10;
    v.b=20;
    printf("%d\n", v.a);
    return 0;
}
Answer: Option B
5. Point out the error in the program?
struct emp
{
    int ecode;
    struct emp *e;
};

Error: in structure declaration
Linker Error
No Error
None of above


Answer: Option C

6.If the two strings are identical, then strcmp() function returns
-1
1
0
Yes
Answer: Option C
Explanation:
Declaration: strcmp(const char *s1, const char*s2);
The strcmp return an int value that is
if s1 < s2 returns a value < 0
if s1 == s2 returns 0
if s1 > s2 returns a value > 0

7.How will you print \n on the screen?
printf("\n");
echo "\\n";
printf('\n');
printf("\\n");
Answer: Option D
Explanation:
The statement printf("\\n"); prints '\n' on the screen.


8.Which of the following function is correct that finds the length of a string?
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    {    length++; s++; }
    return (length);
}
int xstrlen(char s)
{
    int length=0;
    while(*s!='\0')
        length++; s++;
    return (length);
}
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        length++;
    return (length);
}
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
        s++;
    return (length);
}

(a)
9.What function should be used to free the memory allocated by calloc() ?
dealloc();
malloc(variable_name, 0)
free();
memalloc(variable_name, 0)

(c)

10.
How will you free the memory allocated by the following program?
#include<stdio.h>
#include<stdlib.h>
#define MAXROW 3
#define MAXCOL 4

int main()
{
    int **p, i, j;
    p = (int **) malloc(MAXROW * sizeof(int*));
    return 0;
}
memfree(int p);
dealloc(p);
malloc(p, 0);
free(p);

Answer: Option D







11. What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(20); /* Assume p has address of 1314 */
    free(p);
    printf("%u", p);
    return 0;
}

(a)

12. What will be the output of the program (16-bit platform)?
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *p;
    p = (int *)malloc(20);
    printf("%d\n", sizeof(p));
    free(p);
    return 0;
}
Answer: Option A


13. What will be the output of the program?
#include<stdio.h>
#include<string.h>

int main()
{
    char *s;
    char *fun();
    s = fun();
    printf("%s\n", s);
    return 0;
}
char *fun()
{
    char buffer[30];
    strcpy(buffer, "RAM");
    return (buffer);
}

Answer: Option B

14.Point out the error in the following program.
#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *a[3];
    a = (int*) malloc(sizeof(int)*3);
    free(a);
    return 0;
}
Error: unable to allocate memory
Error: We cannot store address of allocated memory in a
Error: unable to free memory
No error

Answer: Option B

15.
malloc() returns a float pointer if memory is allocated for storing float's and adouble pointer if memory is allocated for storing double's.
True
False
Answer: Option B
16. Which is an indirection operator among the following?
a) &
b) *
c) ->
d) .
Answer: Option B
17. Which of the following does not initialize ptr to null (assuming variable declaration of a as int a=0;?
a) int *ptr = &a;
b) int *ptr = &a – &a;
c) int *ptr = a – a;
d) All of the mentioned


18. What is the output of this C code?
    #include <stdio.h>
    int x = 0;
    void main()
    {
        int *ptr = &x;
        printf("%p\n", ptr);
        x++;
        printf("%p\n ", ptr);
    }
a) Same address
b) Different address
c) Compile time error
d) Varies
19. For a typical program, the input is taken using.
a) scanf
b) Files
c) Command-line
d) None of the mentioned
Answer: Option A
20. The value of EOF is_____.
a) -1
b) 0
c) 1
d) 10
Answer: Option A
21. Which is true?
a) The symbolic constant EOF is defined in 
b) The value is typically -1,
c) Both a & b
d) Either a or b
Answer: Option C
22. What is the use of putchar()?
a) The character written
b) EOF is an error occurs
c) Nothing
d) Both a & b
Answer: Option C
23. Which of the following statements about stdout and stderr are true?
a) Same
b) Both connected to screen always.
c) Both connected to screen by default.
d) stdout is line buffered but stderr is unbuffered.
(d)

24. What is the output of this C code?
    #include <stdio.h>
    void main()
    {
        int x = 0;
        int *ptr = &5;
        printf("%p\n", ptr);
    }

(d)

25.
What does fp point to in the program ?
#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    return 0;
}
The first character in the file
A structure which contains a char pointer which points to the first character of a file.
The name of the file.
The last character in the file.
Answer: Option B

26. What does the following segment of code do?
    fprintf(fp, “Copying!”);
a) It writes “Copying!” into the file pointed by fp
b) It reads “Copying!” from the file and prints on display
c) It writes as well as reads “Copying!” to and from the file and prints it
d) None of the mentioned

Answer: Option A

27.What is (void*)0?
Representation of NULL pointer
Representation of void pointer
Error
None of above
Answer: Option A

28.In which header file is the NULL macro defined?
stdio.h
stddef.h
stdio.h and stddef.h
math.h
Answer: Option C
Explanation:
The macro "NULL" is defined in locale.h, stddef.h, stdio.h, stdlib.h, string.h, time.h, and wchar.h.
29. The first and second arguments of fopen are
a) A character string containing the name of the file & the second argument is the mode.
b) A character string containing the name of the user & the second argument is the mode.
c) A character string containing file poniter & the second argument is the mode.
d) None of the mentioned of the mentioned
Answer:a
30. Which of the following mode argument is used to truncate?
a) a
b) f
c) w
d) t
Answer:c

No comments:

Post a Comment