欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C Programing Test And Answer 01

程序员文章站 2024-03-01 16:43:52
...

1.Are the three declarations char **apple, char *apple[], and char apple[][] same?
A. True
B. False

Correct Answer:Option B
Explanation:
char **apple - Pointer to pointer to a character
char *apple[] - An array of pointer to a character
char apple[][]- It is 2 dimensional array

2.Point out the compile time error in the program given below.

#include <stdio.h>
int main(void)
{
    int *x;
    *x=100;
    return 0;
}

A. Error: invalid assignment for x
B. Error: suspicious pointer conversion
C. No error
D. None of above

Correct Answer: Option C
Explanation:
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).

3.Which bitwise operator is suitable for checking whether a particular bit is on or off?
A.&& operator
B. & operator
C. || operator
D. ! operator

Correct Answer: Option B
Explanation:
&& is a Logical operator which combine two or more relation
Ex: let a=5,b=2
(a5) && (b2)
true && true = true
i.e, 1 && 1 = 1
While & is a bitwise operator which has ability to support the manipulation of data at bit level
Ex:let a=9,b=15
Then, a & b
9 & 15
1001 & 1111 = 1001 (use AND operation on each bit)
i.e, ans:- 9
0 | 1 =1
1 | 1 =1.
0 & 1 =0
1 & 1 =1.

4.Point out the error in the program?

#include<stdio.h>

int main()
{
    FILE *fp;
    fp=fopen("trial", "r");
    fseek(fp, "20", SEEK_SET);
    fclose(fp);
    return 0;
}

A. Error: unrecognised Keyword SEEK_SET
B. Error: fseek() long offset value
C. No error
D. None of above

Correct Answer: Option B
Explanation:
Instead of “20” use 20L since fseek() need a long offset value.

5.Bitwise | can be used to multiply a number by powers of 2.
A. Yes
B. No

Correct Answer: Option B

6.Functions cannot return more than one value at a time
A. True
B. False

Correct Answer: Option A
Explanation:
True, A function cannot return more than one value at a time. because after returning a value the control is given back to calling function.

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

Correct Answer: Option A

8.Which of the following statements are correct about the program?

#include<stdio.h>

int main()
{
    unsigned int num;
    int i;
    scanf("%u", &num);
    for(i=0; i<16; i++)
    {
        printf("%d", (num<<i & 1<<15)?1:0);
    }
    return 0;
}

A. It prints all even bits from num
B. It prints all odd bits from num
C. It prints binary equivalent num
D. Error

Correct Answer: Option C
Explanation:
If we give input 4, it will print 00000000 00000100 ;
If we give input 3, it will print 00000000 00000011 ;
If we give input 511, it will print 00000001 11111111 ;

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

Correct Answer: Option C

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

Correct Answer: Option A

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

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

12.What will be the output of the program?

#include<stdio.h>

int main()
{
    int i;
    char c;
    for(i=1; i<=5; i++)
    {
        scanf("%c", &c); /* given input is 'b' */
        ungetc(c, stdout);
        printf("%c", c);
        ungetc(c, stdin);
    }
    return 0;
}

A. bbbb
B. bbbbb
C. b
D. Error in ungetc statement.

Correct Answer: Option C
Explanation:
The ungetc() function pushes the character c back onto the named input stream, which must be open for reading.
This character will be returned on the next call to getc or fread for that stream.
One character can be pushed back in all situations.
A second call to ungetc without a call to getc will force the previous character to be forgotten.

13.Bitwise & can be used to check if more than one bit in a number is on.
A. True
B. False

Correct Answer: Option A

14.In the statement expression1 >> expression2. if expression1 is a signed integer with its leftmost bit set to 1 then on right shifting it the result of the statement will vary from computer to computer
A. True
B. False

Correct Answer: Option A

15.What will be the output of the program?

#include<stdio.h>

int main()
{
    unsigned char i = 0x80;
    printf("%d\n", i<<1);
    return 0;
}

A. 0
B. 256
C. 100
D. 80

Correct Answer: Option B

16.Assunming, integer is 2 byte, What will be the output of the program?

#include<stdio.h>

int main()
{
    printf("%x\n", -1>>1);
    return 0;
}

A. ffff
B. 0fff
C. 0000
D. fff0

Correct Answer: Option A
Explanation:
Negative numbers are treated with 2’s complement method.
1’s complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
2’s complement: Adding 1 to the result of 1’s complement.
Binary of 1(2byte) : 0000 0000 0000 0001
Representing -1:
1s complement of 1(2byte) : 1111 1111 1111 1110
Adding 1 to 1’s comp. result : 1111 1111 1111 1111
Right shift 1bit(-1>>1): 1111 1111 1111 1111 (carry out 1)
Hexadecimal : f f f f
(Filled with 1s in the left side in the above step)
Note:

  1. Fill with 1s in the left side for right shift for negative numbers.
  2. Fill with 0s in the right side for left shift for negative numbers.
  3. Fill with 0s in the left side for right shift for positive numbers.
  4. Fill with 0s in the right side for left shift for positive numbers.

17.What will be the output of the program?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    union test
    {
        int i;
        float f;
        char c;
    };
    union test *t;
    t = (union test *)malloc(sizeof(union test));
    t->f = 10.10f;
    printf("%f", t->f);
    return 0;
}

A. 10
B. Garbage value
C. 10.100000
D. Error

Correct Answer: Option C

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

Correct Answer: Option C

19.The library function used to find the last occurrence of a character in a string is
A. strnstr()
B. laststr()
C. strrchr()
D. strstr()

Correct Answer: Option C
Explanation:
Declaration: char *strrchr(const char *s, int c);
It scans a string s in the reverse direction, looking for a specific character c.
Example:

#include <string.h>
#include <stdio.h>
int main(void)
{
   char text[] = "I learn through IndiaBIX.com";
   char *ptr, c = 'i';
   ptr = strrchr(text, c);
   if (ptr)
      printf("The position of '%c' is: %d\n", c, ptr-text);
   else
      printf("The character was not found\n");
   return 0;
}

Output:

The position of ‘i’ is: 19

20.If the two strings are identical, then strcmp() function returns

A. -1
B. 1
C. 0
D. Yes

Correct Answer: Option C
Explanation:
Declaration: strcmp(const char s1, const chars2);
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

相关标签: sanfoundry