Wednesday, January 6, 2016

C Programming :: Strings

 C Programming :: Strings

1. Which of the following function sets first n characters of a string to a given character?
A.strinit()   
B.strnset()
C.strset()   
D.strcset()

Answer: Option B
Explanation:char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch
#include <stdio.h>
#include <string.h>
int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz";
   char letter = 'x';

   printf("string before strnset: %s\n", string);
   strnset(string, letter, 13);
   printf("string after  strnset: %s\n", string);

   return 0;
}
Output:
string before strnset: abcdefghijklmnopqrstuvwxyz
string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz

2. If the two strings are identical, then strcmp() function returns
A.-1   
B.1
C.0   
D.Yes

3. How will you print \n on the screen?
A.printf("\n");   
B.echo "\\n";
C.printf('\n');   
D.printf("\\n");

Answer: Option D
Explanation:The statement printf("\\n"); prints '\n' on the screen.

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

5. Which of the following function is used to find the first occurrence of a given string in another string?
A.strchr()
B.strrchr()
C.strstr()   
D.strnset()

Answer: Option C
Explanation:The function strstr() Finds the first occurrence of a substring in another string
Declaration: char *strstr(const char *s1, const char *s2);
Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.
Example:
#include <stdio.h>
#include <string.h>
int main(void)
{
   char *str1 = "IndiaBIX", *str2 = "ia", *ptr;

   ptr = strstr(str1, str2);
   printf("The substring is: %s\n", ptr);
   return 0;
}
Output: The substring is: iaBIX

6. Which of the following function is more appropriate for reading in a multi-word string?
A.printf();   
B.scanf();
C.gets();   
D.puts();

Answer: Option C
Explanation:gets(); collects a string of characters terminated by a new line from the standard input stream stdin
#include <stdio.h>
int main(void)
{
   char string[80];
   printf("Enter a string:");
   gets(string);
   printf("The string input was: %s\n", string);
   return 0;
}
Output:
Enter a string: IndiaBIX
The string input was: IndiaBIX

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

Answer: Option A
Explanation:option A is the correct function to find the length of given string.
Example:
#include<stdio.h>
int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    { length++; s++; }
    return (length);
}

int main()
{
    char d[] = "IndiaBIX";
    printf("Length = %d\n", xstrlen(d));
    return 0;
}
Output: Length = 8

8.What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    char str1[20] = "Hello", str2[20] = " World";
    printf("%s\n", strcpy(str2, strcat(str1, str2)));
    return 0;
}

A.Hello   
B.World
C.Hello World   
D.WorldHello

Answer: Option C
Explanation:Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively.
Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.
Hence it prints "Hello World".

9. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char p[] = "%d\n";
    p[1] = 'c';
    printf(p, 65);
    return 0;
}

A.A
B.a
C.c   
D.65

Answer: Option A
Explanation:Step 1: char p[] = "%d\n"; The variable p is declared as an array of characters and initialized with string "%d".
Step 2: p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes "%c".
Step 3: printf(p, 65); becomes printf("%c", 65);
Therefore it prints the ASCII value of 65. The output is 'A'.

10.What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    printf("%d\n", strlen("123456"));
    return 0;
}

A.6   
B.12
C.7
D.2

Answer: Option A
Explanation:The function strlen returns the number of characters in the given string.
Therefore, strlen("123456") returns 6.
Hence the output of the program is "6".

11.What will be the output of the program ?
#include<stdio.h>
int main()
{
    printf(5+"Good Morning\n");
    return 0;
}

A.Good Morning   
B.Good
C.M   
D.Morning

12. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    char str[] = "India\0\BIX\0";
    printf("%s\n", str);
    return 0;
}

A.BIX   
B.India
C.India BIX   
D.India\0BIX

Answer: Option B
Explanation:A string is a collection of characters terminated by '\0'.
Step 1: char str[] = "India\0\BIX\0"; The variable str is declared as an array of characters and initialized with value "India"
Step 2: printf("%s\n", str); It prints the value of the str.
The output of the program is "India".

13. What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied as input?
#include<stdio.h>
int main()
{
    void fun();
    fun();
    printf("\n");
    return 0;
}
void fun()
{
    char c;
    if((c = getchar())!= '\n')
        fun();
    printf("%c", c);
}

A.abc abc   
B.bca
C.Infinite loop   
D.cba

Answer: Option D
Explanation:Step 1: void fun(); This is the prototype for the function fun().
Step 2: fun(); The function fun() is called here.
The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order.
The given input characters are "abc"
Output: cba

14. What will be the output of the program ?
#include<stdio.h>
int main()
{
    printf("India", "BIX\n");
    return 0;
}

A.Error
B.India BIX
C.India   
D.BIX

15. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char str[7] = "IndiaBIX";
    printf("%s\n", str);
    return 0;
}

A.Error   
B.IndiaBIX
C.Cannot predict   
D.None of above

Answer: Option C
Explanation:Here str[] has declared as 7 character array and into a 8 character is stored. This will result in overwriting of the byte beyond 7 byte reserved for '\0'.

16. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"};
    int i;
    char *t;
    t = names[3];
    names[3] = names[4];
    names[4] = t;
    for(i=0; i<=4; i++)
        printf("%s,", names[i]);
    return 0;
}

A.Suresh, Siva, Sona, Baiju, Ritu
B.Suresh, Siva, Sona, Ritu, Baiju
C.Suresh, Siva, Baiju, Sona, Ritu
D.Suresh, Siva, Ritu, Sona, Baiju

Answer: Option B
Explanation:Step 1: char *names[] = { "Suresh", "Siva", "Sona", "Baiju", "Ritu"}; The variable names is declared as an pointer to a array of strings.
Step 2: int i; The variable i is declared as an integer type.
Step 3: char *t; The variable t is declared as pointer to a string.
Step 4: t = names[3]; names[3] = names[4]; names[4] = t; These statements the swaps the 4 and 5 element of the array names.
Step 5: for(i=0; i<=4; i++) printf("%s,", names[i]); These statement prints the all the value of the array names.
Hence the output of the program is "Suresh, Siva, Sona, Ritu, Baiju".

17. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    char str[] = "India\0\BIX\0";
    printf("%d\n", strlen(str));
    return 0;
}

A.10   
B.6
C.5   
D.11

Answer: Option C
Explanation:The function strlen returns the number of characters int the given string.
Therefore, strlen(str) becomes strlen("India") contains 5 characters. A string is a collection of characters terminated by '\0'.
The output of the program is "5".

18. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    static char str1[] = "dills";
    static char str2[20];
    static char str3[] = "Daffo";
    int i;
    i = strcmp(strcat(str3, strcpy(str2, str1)), "Daffodills");
    printf("%d\n", i);
    return 0;
}

A.0   
B.1
C.2   
D.4

Answer: Option A

19. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    static char s[] = "Hello!";
    printf("%d\n", *(s+strlen(s)));
    return 0;
}

A.8   
B.0
C.16   
D.Error

Answer: Option B

20. What will be the output of the program ?
#include<stdio.h>
int main()
{
    static char s[25] = "The cocaine man";
    int i=0;
    char ch;
    ch = s[++i];
    printf("%c", ch);
    ch = s[i++];
    printf("%c", ch);
    ch = i++[s];
    printf("%c", ch);
    ch = ++i[s];
    printf("%c", ch);
    return 0;
}

A.hhe!   
B.he c
C.The c   
D.Hhec

Answer: Option A

21. What will be the output of the program in 16-bit platform (Turbo C under DOS) ?
#include<stdio.h>
int main()
{
    printf("%d, %d, %d", sizeof(3.0f), sizeof('3'), sizeof(3.0));
    return 0;
}

A.8, 1, 4   
B.4, 2, 8
C.4, 2, 4   
D.10, 3, 4

22. What will be the output of the program ?
#include<stdio.h>
int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is empty\n");
    else
        printf("The string is not empty\n");
    return 0;
}

A.The string is empty   
B.The string is not empty
C.No output   
D.0

Answer: Option B
Explanation:The function printf() returns the number of charecters printed on the console.
Step 1: char a[] = "\0"; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.
Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.
In the else part it prints "The string is not empty".

23. If char=1, int=4, and float=4 bytes size, What will be the output of the program ?
#include<stdio.h>
int main()
{
    char ch = 'A';
    printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14f));
    return 0;
}

A.1, 2, 4   
B.1, 4, 4
C.2, 2, 4   
D.2, 4, 8

Answer: Option B
Explanation:Step 1: char ch = 'A'; The variable ch is declared as an character type and initialized with value 'A'.
Step 2:
printf("%d, %d, %d", sizeof(ch), sizeof('A'), sizeof(3.14));
The sizeof function returns the size of the given expression.
sizeof(ch) becomes sizeof(char). The size of char is 1 byte.
sizeof('A') becomes sizeof(65). The size of int is 4 bytes (as mentioned in the question).
sizeof(3.14f). The size of float is 4 bytes.
Hence the output of the program is 1, 4, 4

24. If the size of pointer is 32 bits What will be the output of the program ?
#include<stdio.h>
int main()
{
    char a[] = "Visual C++";
    char *b = "Visual C++";
    printf("%d, %d\n", sizeof(a), sizeof(b));
    printf("%d, %d", sizeof(*a), sizeof(*b));
    return 0;
}

A.10, 22, 2   
B.10, 41, 2
C.11, 41, 1   
D.12, 22, 2

Answer:
Option C

25. What will be the output of the program ?
#include<stdio.h>
int main()
{
    static char mess[6][30] = {"Don't walk in front of me...",
                               "I may not follow;",
                               "Don't walk behind me...",
                               "Just walk beside me...",
                               "And be my friend." };

    printf("%c, %c\n", *(mess[2]+9), *(*(mess+2)+9));
    return 0;
}

A.t, t   
B.k, k
C.n, k   
D.m, f

26. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char str1[] = "Hello";
    char str2[10];
    char *t, *s;
    s = str1;
    t = str2;
    while(*t=*s)
        *t++ = *s++;
    printf("%s\n", str2);
    return 0;
}

A.Hello   
B.HelloHello
C.No output   
D.ello

Answer: Option A

27. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char str[] = "India\0BIX\0";
    printf("%d\n", sizeof(str));
    return 0;
}

A.10   
B.6
C.5   
D.11

Answer: Option D
Explanation:The following examples may help you understand this problem:
1. sizeof("") returns 1 (1*).
2. sizeof("India") returns 6 (5 + 1*).
3. sizeof("BIX") returns 4 (3 + 1*).
4. sizeof("India\0BIX") returns 10 (5 + 1 + 3 + 1*).
    Here '\0' is considered as 1 char by sizeof() function.
5. sizeof("India\0BIX\0") returns 11 (5 + 1 + 3 + 1 + 1*).
    Here '\0' is considered as 1 char by sizeof() function.

28. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char str[25] = "IndiaBIX";
    printf("%s\n", &str+2);
    return 0;
}

A.Garbage value   
B.Error
C.No output   
D.diaBIX

Answer: Option A
Explanation:Step 1: char str[25] = "IndiaBIX"; The variable str is declared as an array of characteres and initialized with a string "IndiaBIX".
Step 2: printf("%s\n", &str+2);
=> In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2
=> &str is a location of string "IndiaBIX". Therefore &str+2 is another memory location.
Hence it prints the Garbage value.

29. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char str = "IndiaBIX";
    printf("%s\n", str);
    return 0;
}

A.Error   
B.IndiaBIX
C.Base address of str   
D.No output

Answer: Option A
Explanation:The line char str = "IndiaBIX"; generates "Non portable pointer conversion" error.
To eliminate the error, we have to change the above line to
char *str = "IndiaBIX"; (or) char str[] = "IndiaBIX";
Then it prints "IndiaBIX".

30. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char str[] = "Nagpur";
    str[0]='K';
    printf("%s, ", str);
    str = "Kanpur";
    printf("%s", str+1);
    return 0;
}

A.Kagpur, Kanpur   
B.Nagpur, Kanpur
C.Kagpur, anpur   
D.Error

Answer: Option D
Explanation:The statement str = "Kanpur"; generates the LVALUE required error. We have to use strcpy function to copy a string.
To remove error we have to change this statement str = "Kanpur"; to strcpy(str, "Kanpur");
The program prints the string "anpur"

31. What will be the output of the program ?
#include<stdio.h>
int main()
{
    printf(5+"IndiaBIX\n");
    return 0;
}

A.Error   
B.IndiaBIX
C.BIX   
D.None of above

Answer: Option C
Explanation:printf(5+"IndiaBIX\n"); In the printf statement, it skips the first 5 characters and it prints "BIX"

32. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    char sentence[80];
    int i;
    printf("Enter a line of text\n");
    gets(sentence);
    for(i=strlen(sentence)-1; i >=0; i--)
        putchar(sentence[i]);
    return 0;
}

A.The sentence will get printed in same order as it entered
B.The sentence will get printed in reverse order
C.Half of the sentence will get printed
D.None of above

Answer: Option B

33. What will be the output of the program ?
#include<stdio.h>
void swap(char *, char *);
int main()
{
    char *pstr[2] = {"Hello", "IndiaBIX"};
    swap(pstr[0], pstr[1]);
    printf("%s\n%s", pstr[0], pstr[1]);
    return 0;
}
void swap(char *t1, char *t2)
{
    char *t;
    t=t1;
    t1=t2;
    t2=t;
}

A.IndiaBIXHello
B.Address of "Hello" and "IndiaBIX"
C.HelloIndiaBIX   
D.IelloHndiaBIX

Answer: Option C
Explanation:Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything.
Step 2: char *pstr[2] = {"Hello", "IndiaBIX"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to
pstr[0] = "Hello", pstr[1] = "IndiaBIX"
Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program.
If the swap function is "called by reference" it will affect the variable pstr.
Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1].
Hence the output of the program is
Hello
IndiaBIX

34. What will be the output of the program (Turbo C in 16 bit platform DOS) ?
#include<stdio.h>
#include<string.h>
int main()
{
    char *str1 = "India";
    char *str2 = "BIX";
    char *str3;
    str3 = strcat(str1, str2);
    printf("%s %s\n", str3, str1);
    return 0;
}

A.IndiaBIX India   
B.IndiaBIX IndiaBIX
C.India India   
D.Error

Answer: Option B
Explanation:It prints 'IndiaBIX IndiaBIX' in TurboC (in 16 bit platform).
It may cause a 'segmentation fault error' in GCC (32 bit platform).

35. If the size of pointer is 4 bytes then What will be the output of the program ?
#include<stdio.h>
int main()
{
    char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"};
    printf("%d, %d", sizeof(str), strlen(str[0]));
    return 0;
}

A.22, 4   
B.25, 5
C.24, 5   
D.20, 2

Answer: Option C
Explanation:Step 1: char *str[] = {"Frogs", "Do", "Not", "Die", "They", "Croak!"}; The variable str is declared as an pointer to the array of 6 strings.
Step 2: printf("%d, %d", sizeof(str), strlen(str[0]));
sizeof(str) denotes 6 * 4 bytes = 24 bytes. Hence it prints '24'
strlen(str[0])); becomes strlen(Frogs)). Hence it prints '5';
Hence the output of the program is 24, 5
Hint: If you run the above code in 16 bit platform (Turbo C under DOS) the output will be 12, 5. Because the pointer occupies only 2 bytes. If you run the above code in Linux (32 bit platform), the output will be 24, 5 (because the size of pointer is 4 bytes).

36. What will be the output of the program ?
#include<stdio.h>
int main()
{
    int i;
    char a[] = "\0";
    if(printf("%s", a))
        printf("The string is not empty\n");
    else
        printf("The string is empty\n");
    return 0;
}

A.The string is not empty   
B.The string is empty
C.No output   
D.0

Answer: Option B
Explanation:The function printf() returns the number of charecters printed on the console.
Step 1: char a[] = '\0'; The variable a is declared as an array of characters and it initialized with "\0". It denotes that the string is empty.
Step 2: if(printf("%s", a)) The printf() statement does not print anything, so it returns '0'(zero). Hence the if condition is failed.
In the else part it prints "The string is empty".

37. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    char str1[5], str2[5];
    int i;
    gets(str1);
    gets(str2);
    i = strcmp(str1, str2);
    printf("%d\n", i);
    return 0;
}

A.Unpredictable integer value
B.0
C.-1   
D.Error

Answer: Option A
Explanation:gets() gets collects a string of characters terminated by a new line from the standard input stream stdin.
The gets(str1) read the input string from user and store in variable str1.
The gets(str2) read the input string from user and store in variable str2.
The code i = strcmp(str1, str2); The strcmp not only returns -1, 0 and +1, but also other negative or positive values. So the value of i is "unpredictable integer value".
printf("%d\n", i); It prints the value of variable i.

38.What will be the output of the program in Turbo C?
#include<stdio.h>
int main()
{
    char str[10] = "India";
    str[6] = "BIX";
    printf("%s\n", str);
    return 0;
}

A.India BIX   
B.BIX
C.India   
D.Error

Answer: Option D
Explanation:str[6] = "BIX"; - Nonportable pointer conversion.

39. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char str1[] = "Hello";
    char str2[] = "Hello";
    if(str1 == str2)
        printf("Equal\n");
    else
        printf("Unequal\n");
    return 0;
}

A.Equal
B.Unequal
C.Error   
D.None of above

Answer: Option B
Explanation:Step 1: char str1[] = "Hello"; The variable str1 is declared as an array of characters and initialized with a string "Hello".
Step 2: char str2[] = "Hello"; The variable str2 is declared as an array of characters and initialized with a string "Hello".
We have use strcmp(s1,s2) function to compare strings.
Step 3: if(str1 == str2) here the address of str1 and str2 are compared. The address of both variable is not same. Hence the if condition is failed.
Step 4: At the else part it prints "Unequal".

40. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char t;
    char *p1 = "India", *p2;
    p2=p1;
    p1 = "BIX";
    printf("%s %s\n", p1, p2);
    return 0;
}

A.India BIX   
B.BIX India
C.India India   
D.BIX BIX

Answer: Option B
Explanation:Step 1: char *p1 = "India", *p2; The variable p1 and p2 is declared as an pointer to a character value and p1 is assigned with a value "India".
Step 2: p2=p1; The value of p1 is assigned to variable p2. So p2 contains "India".
Step 3: p1 = "BIX"; The p1 is assigned with a string "BIX"
Step 4: printf("%s %s\n", p1, p2); It prints the value of p1 and p2.
Hence the output of the program is "BIX India".

41. What will be the output of the program ?
#include<stdio.h>
#include<string.h>
int main()
{
    printf("%c\n", "abcdefgh"[4]);
    return 0;
}

A.Error   
B.d
C.e   
D.abcdefgh

Answer: Option C
Explanation:printf("%c\n", "abcdefgh"[4]); It prints the 5 character of the string "abcdefgh".
Hence the output is 'e'.

42. What will be the output of the following program in 16 bit platform assuming that 1022 is memory address of the string "Hello1" (in Turbo C under DOS) ?
#include<stdio.h>
int main()
{
    printf("%u %s\n", &"Hello1", &"Hello2");
    return 0;
}

A.1022 Hello2   
B.Hello1 1022
C.Hello1 Hello2   
D.1022 1022
E.Error       

Answer: Option A
Explanation:In printf("%u %s\n", &"Hello", &"Hello");.
The %u format specifier tells the compiler to print the memory address of the "Hello1".
The %s format specifier tells the compiler to print the string "Hello2".
Hence the output of the program is "1022 Hello2".

C Programming :: Arrays

C Programming :: Arrays
 
1. What will happen if in a C program you assign a value to an array element whose subscript exceeds the size of array?
A.The element will be set to 0.
B.The compiler would report an error.
C.The program may crash if some important data gets overwritten.
D.The array size would appropriately grow.

Answer: Option C
Explanation:If the index of the array size is exceeded, the program will crash. Hence "option c" is the correct answer. But the modern compilers will take care of this kind of errors.
Example: Run the below program, it will crash in Windows (TurboC Compiler)
#include<stdio.h>
int main()
{
    int arr[2];
    arr[3]=10;
    printf("%d",arr[3]);
    return 0;
}
Since C is a compiler dependent language, it may give different outputs at different platforms. We have given the Turbo-C Compiler (Windows) output.
Please try the above programs in Windows (Turbo-C Compiler) and Linux (GCC Compiler), you will understand the difference better.

2. What does the following declaration mean?
int (*ptr)[10];
A.ptr is array of pointers to 10 integers
B.ptr is a pointer to an array of 10 integers
C.ptr is an array of 10 integers
D.ptr is an pointer to array

Answer: Option B

3. In C, if you pass an array as an argument to a function, what actually gets passed?
A.Value of elements in array
B.First element of the array
C.Base address of the array
D.Address of the last element of array

Answer: Option C
Explanation:The statement 'C' is correct. When we pass an array as a funtion argument, the base address of the array will be passed.


4. What will be the output of the program ?

#include<stdio.h>
int main()
{
    int a[5] = {5, 1, 15, 20, 25};
    int i, j, m;
    i = ++a[1];
    j = a[1]++;
    m = a[i++];
    printf("%d, %d, %d", i, j, m);
    return 0;
}

 A.2, 1, 15 
 B.1, 2, 5
C.3, 2, 15   
D.2, 3, 20

Answer: Option C
Explanation:Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to
a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 .
Step 2: int i, j, m; The variable i,j,m are declared as an integer type.
Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2
Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3.
Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3)
Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m
Hence the output of the program is 3, 2, 15

5. What will be the output of the program ?
#include<stdio.h>
int main()
{
    static int a[2][2] = {1, 2, 3, 4};
    int i, j;
    static int *p[] = {(int*)a, (int*)a+1, (int*)a+2};
    for(i=0; i<2; i++)
    {
        for(j=0; j<2; j++)
        {
            printf("%d, %d, %d, %d\n", *(*(p+i)+j), *(*(j+p)+i),
                                    *(*(i+p)+j), *(*(p+j)+i));
        }
    }
    return 0;
}

A.1, 1, 1, 1
  2, 3, 2, 3
  3, 2, 3, 2
  4, 4, 4, 4   
B.1, 2, 1, 2
  2, 3, 2, 3
  3, 4, 3, 4
  4, 2, 4, 2
C.1, 1, 1, 1
  2, 2, 2, 2
  2, 2, 2, 2
  3, 3, 3, 3   
D.1, 2, 3, 4
  2, 3, 4, 1
  3, 4, 1, 2
  4, 1, 2, 3

Answer: Option C

6. What will be the output of the program ?
#include<stdio.h>
int main()
{
    void fun(int, int[]);
    int arr[] = {1, 2, 3, 4};
    int i;
    fun(4, arr);
    for(i=0; i<4; i++)
        printf("%d,", arr[i]);
    return 0;
}
void fun(int n, int arr[])
{
    int *p=0;
    int i=0;
    while(i++ < n)
        p = &arr[i];
    *p=0;
}

A.2, 3, 4, 5   
B.1, 2, 3, 4
C.0, 1, 2, 3   
D.3, 2, 1 0

Answer: Option B
Explanation:Step 1: void fun(int, int[]); This prototype tells the compiler that the function fun() accepts one integer value and one array as an arguments and does not return anything.
Step 2: int arr[] = {1, 2, 3, 4}; The variable a is declared as an integer array and it is initialized to
a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4
Step 3: int i; The variable i is declared as an integer type.
Step 4: fun(4, arr); This function does not affect the output of the program. Let's skip this function.
Step 5: for(i=0; i<4; i++) { printf("%d,", arr[i]); } The for loop runs untill the variable i is less than '4' and it prints the each value of array a.
Hence the output of the program is 1,2,3,4

7. What will be the output of the program ?
#include<stdio.h>
void fun(int **p);
int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0};
    int *ptr;
    ptr = &a[0][0];
    fun(&ptr);
    return 0;
}
void fun(int **p)
{
    printf("%d\n", **p);
}

A.1   
B.2
C.3   
D.4

Answer:
Option A
Explanation:Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 8, 7, 8, 9, 0}; The variable a is declared as an multidimensional integer array with size of 3 rows 4 columns.
Step 2: int *ptr; The *ptr is a integer pointer variable.
Step 3: ptr = &a[0][0]; Here we are assigning the base address of the array a to the pointer variable *ptr.
Step 4: fun(&ptr); Now, the &ptr contains the base address of array a.
Step 4: Inside the function fun(&ptr); The printf("%d\n", **p); prints the value '1'.
because the *p contains the base address or the first element memory address of the array a (ie. a[0])
**p contains the value of *p memory location (ie. a[0]=1).
Hence the output of the program is '1'

8.    What will be the output of the program ?
#include<stdio.h>
int main()
{
    static int arr[] = {0, 1, 2, 3, 4};
    int *p[] = {arr, arr+1, arr+2, arr+3, arr+4};
    int **ptr=p;
    ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *ptr++;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    *++ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    ++*ptr;
    printf("%d, %d, %d\n", ptr-p, *ptr-arr, **ptr);
    return 0;
}

    A.0, 0, 0
       1, 1, 1
        2, 2, 2
       3, 3, 3  
 B.1, 1, 2
     2, 2, 3
     3, 3, 4
     4, 4, 1
C.1, 1, 1
    2, 2, 2
    3, 3, 3
    3, 4, 4 
  D.0, 1, 2
     1, 2, 3
     2, 3, 4
    3, 4, 5

Answer: Option C

9.   What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes?
#include<stdio.h>
int main()
{
    int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0};
    printf("%u, %u\n", a+1, &a+1);
    return 0;
}

 A.65474, 65476   
B.65480, 65496
C.65480, 65488 
 D.65474, 65488

Answer: Option B
Explanation:Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.
Step 2: printf("%u, %u\n", a+1, &a+1);
The base address(also the address of the first element) of array is 65472.
For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480
Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes".
Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496
Hence the output of the program is 65480, 65496

10. What will be the output of the program in Turb C (under DOS)?
#include<stdio.h>
int main()
{
    int arr[5], i=0;
    while(i<5)
        arr[i]=++i;

    for(i=0; i<5; i++)
        printf("%d, ", arr[i]);

    return 0;
}

  A.1, 2, 3, 4, 5,  
 B.Garbage value, 1, 2, 3, 4,
 C.0, 1, 2, 3, 4,  
 D.2, 3, 4, 5, 6,

11. What will be the output of the program ?
#include<stdio.h>
int main()
{
    int arr[1]={10};
    printf("%d\n", 0[arr]);
    return 0;
}

    A.1 
  B.10
C.0 
  D.6

Answer: Option B
Explanation:Step 1: int arr[1]={10}; The variable arr[1] is declared as an integer array with size '2' and it's first element is initialized to value '10'(means arr[0]=10)
Step 2: printf("%d\n", 0[arr]); It prints the first element value of the variable arr.
Hence the output of the program is 10.

12. What will be the output of the program if the array begins at address 65486?
#include<stdio.h>
int main()
{
    int arr[] = {12, 14, 15, 23, 45};
    printf("%u, %u\n", arr, &arr);
    return 0;
}

  A.65486, 65488
   B.65486, 65486
C.65486, 65490  
 D.65486, 65487

13.     What will be the output of the program ?
#include<stdio.h>
int main()
{
    float arr[] = {12.4, 2.3, 4.5, 6.7};
    printf("%d\n", sizeof(arr)/sizeof(arr[0]));
    return 0;
}

   A.5 
   B.4
  C.6  
  D.7

Answer: Option B
Explanation:The sizeof function return the given variable. Example: float a=10; sizeof(a) is 4 bytes
Step 1: float arr[] = {12.4, 2.3, 4.5, 6.7}; The variable arr is declared as an floating point array and it is initialized with the values.
Step 2: printf("%d\n", sizeof(arr)/sizeof(arr[0]));
The variable arr has 4 elements. The size of the float variable is 4 bytes.
Hence 4 elements x 4 bytes = 16 bytes
sizeof(arr[0]) is 4 bytes
Hence 16/4 is 4 bytes
Hence the output of the program is '4'.

14.  What will be the output of the program if the array begins 1200 in memory?
#include<stdio.h>
int main()
{
    int arr[]={2, 3, 4, 1, 6};
    printf("%u, %u, %u\n", arr, &arr[0], &arr);
    return 0;
}

  A.1200, 1202, 1204  
 B.1200, 1200, 1200
 C.1200, 1204, 1208 
 D.1200, 1202, 1200

Answer: Option B
Explanation:Step 1: int arr[]={2, 3, 4, 1, 6}; The variable arr is declared as an integer array and initialized.
Step 2: printf("%u, %u, %u\n", arr, &arr[0], &arr); Here,
The base address of the array is 1200.
=> arr, &arr is pointing to the base address of the array arr.
=> &arr[0] is pointing to the address of the first element array arr. (ie. base address)
Hence the output of the program is 1200, 1200, 1200

Tuesday, January 5, 2016

C Programming - Discussion


C Programming :: Pointers - Discussion


1. What is (void*)0?
[A]. Representation of NULL pointer   
[B]. Representation of void pointer
[C]. Error
[D]. None of above

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);

Answer: Option C

3. In which header file is the NULL macro defined?
A.stdio.h
B.stddef.h
C.stdio.h and stddef.h   
D.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.

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=4 huge=8

Answer: Option A 
Explanation:near=2, far=4 and huge=4 pointers exist only under DOS. Under windows and Linux every pointers is 4 bytes long.

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

Answer: Option D 
Explanation:No answer description available for this question. 

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)

 Answer: Option B
Explanation:No answer description available for this question.

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 

Answer: Option C
Explanation:No answer description available for this question. Let us discuss.

8. The operator used to get value at address stored in a pointer variable is
A.*   
B.&
C.&&   
D.|| 

Answer: Option A 
Explanation:No answer description available for this questions

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

Answer: Option A

10. What will be the output of the program ?
#include<stdio.h>
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

Answer: Option A

11. What will be the output of the program ?
#include<stdio.h>
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

Answer: Option D

12. What will be the output of the program ?
#include<stdio.h>
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

Answer: Option A

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

Answer:
Option A

14. What will be the output of the program ?
#include<stdio.h>
void fun(void *p);
int i;
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

Answer: Option C


15. What will be the output of the program ?

#include<stdio.h>
int main()
{
    char *str;
    str = "%s";
    printf(str, "K\n");
    return 0;
}

A.Error   
B.No output
C.K   
D.%s

Answer: Option C

16.What will be the output of the program ?
#include<stdio.h>
int *check(static int, static int);
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

Answer: Option D

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

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.
This difference is due to the platform dependency of C compiler.

18.What will be the output of the program ?
#include<stdio.h>
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

Answer:
Option D

19.What will be the output of the program?
#include<stdio.h>
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

Answer: Option A

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

Answer: Option C


21. What will be the output of the program?
#include<stdio.h>
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

Answer: Option B

22.hat will be the output of the program ?
#include<stdio.h>
int main()
{
    char *str;
    str = "%d\n";
    str++;
    str++;
    printf(str-2, 300);
    return 0;
}

A.No output   
B.30
C.3   
D.300

Answer: Option D


23. What will be the output of the program ?
#include<stdio.h>
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"

Answer: Option C

24. What will be the output of the program ?

#include<stdio.h>
int main()
{
    char str[] = "peace";
    char *s = str;
    printf("%s\n", s++ +3);
    return 0;
}

A.peace   
B.eace
C.ace   
D.ce

Answer: Option D

25. What will be the output of the program ?
#include<stdio.h>
int main()
{
    char *p;
    p="hello";
    printf("%s\n", *&*&p);
    return 0;
}

A.llo   
B.hello
C.ello   
D.h

Answer:
Option B


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

Answer: Option C
 

27. What will be the output of the program ?
#include<stdio.h>
power(int**);
int main()
{
    int a=5, *aa; /* Address of 'a' is 1000 */
    aa = &a;
    a = power(&aa);
    printf("%d\n", a);
    return 0;
}
power(int **ptr)
{
    int b;
    b = **ptr***ptr;
    return (b);
}

A.5  
B.25
C.125  
D.Garbage value

Answer: Option B

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

Answer: Option B

29.What will be the output of the program ?
#include<stdio.h>
#include<string.h>
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

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

30.What will be the output of the program ?
#include<stdio.h>
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;
}
void 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

Answer: Option B

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

Answer: Option B

32. 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;
}

A.3, 2, 515  
B.515, 2, 3
C.3, 2, 5  
D.515, 515, 4

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.

33. What will be the output of the program ?
#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;
}

A.10  
B.20
C.30  
D.0

Answer: Option B

34. What will be the output of the program ?
#include<stdio.h>
int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit={1, 2, 13};

    printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4);
    return 0;
}

A.1, 2, 13  
B.1, 4, 4
C.-1, 2, -3  
D.-1, -2, -13

Answer: Option C
Explanation:Note the below statement inside the struct:
int bit1:1; --> 'int' indicates that it is a SIGNED integer.
For signed integers the leftmost bit will be taken for +/- sign.
If you store 1 in 1-bit field:
he left most bit is 1, so the system will treat the value as negative number.
The 2's complement method is used by the system to handle the negative values.
herefore, the data stored is 1. The 2's complement of 1 is also 1 (negative).
herefore -1 is printed.If you store 2 in 4-bits field:Binary 2: 0010 (left most bit is 0, so system will treat it as positive value)
0010 is 2 Therefore 2 is printed.f you store 13 in 4-bits field:Binary 13: 1101 (left most bit is 1, so system will treat it as negative value)
Find 2's complement of 1101:
1's complement of 1101 : 0010
2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)
0011 is 3 (but negative value)Therefore -3 is printed.

35.what will be the output of the program in 16 bit platform (Turbo C under DOS) ?
#include<stdio.h>
int main()
{
    struct value
    {
        int bit1:1;
        int bit3:4;
        int bit4:4;
    }bit;
    printf("%d\n", sizeof(bit));
    return 0;
}

A.1  
B.2
C.4  
D.9

Answer: Option B
Explanation:Since C is a compiler dependent language, in Turbo C (DOS) the output will be 2, but in GCC (Linux) the output will be 4.

36. What will be the output of the program ?
#include<stdio.h>
int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d\n", MON, TUE, WED, THU, FRI, SAT);
    return 0;
}

A.-1, 0, 1, 2, 3, 4  
B.-1, 2, 6, 3, 4, 5
C.-1, 0, 6, 2, 3, 4  
D.-1, 0, 6, 7, 8, 9

Answer: Option D
Explanation:No answer description available for this question. Let us discuss.
 

37. What will be the output of the program ?
#include<stdio.h>
int main()
{
    enum status {pass, fail, absent};
    enum status stud1, stud2, stud3;
    stud1 = pass;
    stud2 = absent;
    stud3 = fail;
    printf("%d %d %d\n", stud1, stud2, stud3);
    return 0;
}

A.0, 1, 2  
B.1, 2, 3
C.0, 2, 1  
D.1, 3, 2

Answer: Option C

38. What will be the output of the program ?
#include<stdio.h>
int main()
{
    int i=4, j=8;
    printf("%d, %d, %d\n", i|j&j|i, i|j&j|i, i^j);
    return 0;
}

A.12, 12, 12  
B.112, 1, 12
C.32, 1, 12  
D.-64, 1, 12

Answer: Option A

39. What will be the output of the program in Turbo C (under DOS)?
#include<stdio.h>
int main()
{
    struct emp
    {
        char *n;
        int age;
    };
    struct emp e1 = {"Dravid", 23};
    struct emp e2 = e1;
    strupr(e2.n);
    printf("%s\n", e1.n);
    return 0;
}

A.Error: Invalid structure assignment
B.DRAVID
C.Dravid
D.No output

Answer: Option B

40. What will be the output of the program in 16-bit platform (under DOS)?
#include<stdio.h>
int main()
{
    struct node
    {
        int data;
        struct node *link;
    };
    struct node *p, *q;
    p = (struct node *) malloc(sizeof(struct node));
    q = (struct node *) malloc(sizeof(struct node));
    printf("%d, %d\n", sizeof(p), sizeof(q));
    return 0;
}

A.2, 2  
B.8, 8
C.5, 5  
D.4, 4



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

int main()
{
    struct byte
    {
        int one:1;
    };
    struct byte var = {1};
    printf("%d\n", var.one);
    return 0;
}

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

Answer: Option B
Explanation:No answer description available for this question. Let us discuss.
 

42. What will be the output of the program ?
#include<stdio.h>
int main()
{
    enum days {MON=-1, TUE, WED=6, THU, FRI, SAT};
    printf("%d, %d, %d, %d, %d, %d\n", ++MON, TUE, WED, THU, FRI, SAT);
    return 0;
}

A.-1, 0, 1, 2, 3, 4  
B.Error
C.0, 1, 6, 3, 4, 5  
D.0, 0, 6, 7, 8, 9

Answer: Option B
Explanation:Because ++ or -- cannot be done on enum value.

43. What will be the output of the program ?
#include<stdio.h>
    struct course
    {
        int courseno;
        char coursename[25];
    };
int main()
{
    struct course c[] = { {102, "Java"},
                          {103, "PHP"},
                          {104, "DotNet"}     };

    printf("%d ", c[1].courseno);
    printf("%s\n", (*(c+2)).coursename);
    return 0;
}

A.103 DotNet  
B.102 Java
C.103 PHP  
D.104 DotNet

Answer: Option A


44.   What will be the output of the program given below in 16-bit platform ?
#include<stdio.h>
int main()
{
    enum value{VAL1=0, VAL2, VAL3, VAL4, VAL5} var;
    printf("%d\n", sizeof(var));
    return 0;
}

A.1  
B.2
C.4  
D.10

Answer: Option B
Explanation:No answer description available for this question. Let us discuss.
 

45. The keyword used to transfer control from a function back to the calling function is
A.switch  
B.goto
C.go back  
D.return

46. What is the notation for following functions?
1.  int f(int a, float b)
    {
        /* Some code */
    }

2.  int f(a, b)
    int a; float b;
    {
        /* Some code */
    }

A. 1. KR Notation
   2. ANSI Notation  
B. 1. Pre ANSI C Notation
   2. KR Notation
C. 1. ANSI Notation
   2. KR Notation  
D. 1. ANSI Notation
   2. Pre ANSI Notation

Answer: Option C
Explanation:KR Notation means Kernighan and Ritche Notation.

 47.How many times the program will print "IndiaBIX" ?
#include<stdio.h>
int main()
{
    printf("IndiaBIX");
    main();
    return 0;
}

A.Infinite times  
B.32767 times
C.65535 times  
D.Till stack overflows

Answer: Option D
Explanation:A call stack or function stack is used for several related purposes, but the main reason for having one is to keep track of the point to which each active subroutine should return control when it finishes executing.
A stack overflow occurs when too much memory is used on the call stack.
Here function main() is called repeatedly and its return address is stored in the stack. After stack memory is full. It shows stack overflow error.

48. What will be the output of the program in 16 bit platform (Turbo C under DOS)?
#include<stdio.h>
int main()
{
    int fun();
    int i;
    i = fun();
    printf("%d\n", i);
    return 0;
}
int fun()
{
    _AX = 1990;
}

A.Garbage value  
B.0 (Zero)
C.1990  
D.No output

Answer: Option C
Explanation:Turbo C (Windows): The return value of the function is taken from the Accumulator _AX=1990.
But it may not work as expected in GCC compiler (Linux).


49. What will be the output of the program?
#include<stdio.h>
void fun(int*, int*);
int main()
{
    int i=5, j=2;
    fun(&i, &j);
    printf("%d, %d", i, j);
    return 0;
}
void fun(int *i, int *j)
{
    *i = *i**i;
    *j = *j**j;
}
A.5, 2  
B.10, 4
C.2, 5  
D.25, 4

Answer: Option D
Explanation: Step 1: int i=5, j=2; Here variable i and j are declared as an integer type and initialized to 5 and 2 respectively.
Step 2: fun(&i, &j); Here the function fun() is called with two parameters &i and &j (The & denotes call by reference. So the address of the variable i and j are passed. )
Step 3: void fun(int *i, int *j) This function is called by reference, so we have to use * before the parameters.
Step 4: *i = *i**i; Here *i denotes the value of the variable i. We are multiplying 5*5 and storing the result 25 in same variable i.
Step 5: *j = *j**j; Here *j denotes the value of the variable j. We are multiplying 2*2 and storing the result 4 in same variable j.
Step 6: Then the function void fun(int *i, int *j) return back the control back to main() function.
Step 7: printf("%d, %d", i, j); It prints the value of variable i and j.
Hence the output is 25, 4.

50.What will be the output of the program?
#include<stdio.h>
int i;
int fun();

int main()
{
    while(i)
    {
        fun();
        main();
    }
    printf("Hello\n");
    return 0;
}
int fun()
{
    printf("Hi");
}

A.Hello  
B.Hi Hello
C.No output
D.Infinite loop

Answer: Option A
Explanation:Step 1: int i; The variable i is declared as an integer type.
Step 1: int fun(); This prototype tells the compiler that the function fun() does not accept any arguments and it returns an integer value.
Step 1: while(i) The value of i is not initialized so this while condition is failed. So, it does not execute the while block.
Step 1: printf("Hello\n"); It prints "Hello".
Hence the output of the program is "Hello".



51. What will be the output of the program?
#include<stdio.h>
int reverse(int);
int main()
{
    int no=5;
    reverse(no);
    return 0;
}
int reverse(int no)
{
    if(no == 0)
        return 0;
    else
        printf("%d,", no);
    reverse (no--);
}

A.Print 5, 4, 3, 2, 1   
B.Print 1, 2, 3, 4, 5
C.Print 5, 4, 3, 2, 1, 0   
D.Infinite loop

52.What will be the output of the program?
#include<stdio.h>
void fun(int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
    int a=3;
    fun(a);
    return 0;
}
void fun(int n)
{
    if(n > 0)
    {
        fun(--n);
        printf("%d,", n);
        fun(--n);
    }
}

A.0, 2, 1, 0,   
B.1, 1, 2, 0,
C.0, 1, 0, 2,   
D.0, 1, 2, 0,

Answer: Option D

53.What will be the output of the program?
#include<stdio.h>
int sumdig(int);
int main()
{
    int a, b;
    a = sumdig(123);
    b = sumdig(123);
    printf("%d, %d\n", a, b);
    return 0;
}
int sumdig(int n)
{
    int s, d;
    if(n!=0)
    {
        d = n%10;
        n = n/10;
        s = d+sumdig(n);
    }
    else
        return 0;
    return s;
}

A.4, 4   
B.3, 3
C.6, 6   
D.12, 12

Answer: Option C
 

54.What will be the output of the program?
#include<stdio.h>
int main()
{
    void fun(char*);
    char a[100];
    a[0] = 'A'; a[1] = 'B';
    a[2] = 'C'; a[3] = 'D';
    fun(&a[0]);
    return 0;
}
void fun(char *a)
{
    a++;
    printf("%c", *a);
    a++;
    printf("%c", *a);
}

A.AB   
B.BC
C.CD   
D.No output

Answer: Option B

55.What will be the output of the program?
#include<stdio.h>
int main()
{
    int fun(int);
    int i = fun(10);
    printf("%d\n", --i);
    return 0;
}
int fun(int i)
{
   return (i++);
}

A.9   
B.10
C.11   
D.8

Answer: Option A
Explanation:Step 1: int fun(int); Here we declare the prototype of the function fun().
Step 2: int i = fun(10); The variable i is declared as an integer type and the result of the fun(10) will be stored in the variable i.
Step 3: int fun(int i){ return (i++); } Inside the fun() we are returning a value return(i++). It returns 10. because i++ is the post-increement operator.
Step 4: Then the control back to the main function and the value 10 is assigned to variable i.
Step 5: printf("%d\n", --i); Here --i denoted pre-increement. Hence it prints the value 9.


56. What will be the output of the program?
#include<stdio.h>
int check (int, int);
int main()
{
    int c;
    c = check(10, 20);
    printf("c=%d\n", c);
    return 0;
}
int check(int i, int j)
{
    int *p, *q;
    p=&i;
    q=&j;
    i>=45 ? return(*p): return(*q);
}

A.Print 10   
B.Print 20
C.Print 1   
D.Compile error

Answer: Option D
Explanation:There is an error in this line i>=45 ? return(*p): return(*q);. We cannot use return keyword in the terenary operators.

57. What will be the output of the program?
#include<stdio.h>
int fun(int, int);
typedef int (*pf) (int, int);
int proc(pf, int, int);
int main()
{
    printf("%d\n", proc(fun, 6, 6));
    return 0;
}
int fun(int a, int b)
{
   return (a==b);
}
int proc(pf p, int a, int b)
{
   return ((*p)(a, b));
}

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

Answer: Option B


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

int main()
{
    int i=1;
    if(!i)
        printf("IndiaBIX,");
    else
    {
        i=0;
        printf("C-Program");
        main();
    }
    return 0;
}

A.prints "IndiaBIX, C-Program" infinitely
B.prints "C-Program" infinetly
C.prints "C-Program, IndiaBIX" infinitely
D.Error: main() should not inside else statement

Answer: Option B
Explanation:Step 1: int i=1; The variable i is declared as an integer type and initialized to 1(one).
Step 2: if(!i) Here the !(NOT) operator reverts the i value 1 to 0. Hence the if(0) condition fails. So it goes to else part.
Step 3: else { i=0; In the else part variable i is assigned to value 0(zero).
Step 4: printf("C-Program"); It prints the "C-program".
Step 5: main(); Here we are calling the main() function.
After calling the function, the program repeats from step 1 to step 5 infinitely.
Hence it prints "C-Program" infinitely.
View Answer C Compiler Report Discuss in Forum

59. What will be the output of the program?
#include<stdio.h>
int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}
int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d %d\n", k, l);
    return 0;
}

A.12 12
B.No error, No output
C.Error: Compile error
D.None of above

Answer: Option A

60.What will be the output of the program?
#include<stdio.h>
int i;
int fun1(int);
int fun2(int);
int main()
{
    extern int j;
    int i=3;
    fun1(i);
    printf("%d,", i);
    fun2(i);
    printf("%d", i);
    return 0;
}
int fun1(int j)
{
    printf("%d,", ++j);
    return 0;
}
int fun2(int i)
{
    printf("%d,", ++i);
    return 0;
}
int j=1;

A.3, 4, 4, 3   
B.4, 3, 4, 3
C.3, 3, 4, 4   
D.3, 4, 3, 4

Answer: Option B
Explanation:Step 1: int i; The variable i is declared as an global and integer type.
Step 2: int fun1(int); This prototype tells the compiler that the fun1() accepts the one integer parameter and returns the integer value.
Step 3: int fun2(int); This prototype tells the compiler that the fun2() accepts the one integer parameter and returns the integer value.
Step 4: extern int j; Inside the main function, the extern variable j is declared and defined in another source file.
Step 5: int i=3; The local variable i is defines as an integer type and initialized to 3.
Step 6: fun1(i); The fun1(i) increements the given value of variable i prints it. Here fun1(i) becomes fun1(3) hence it prints '4' then the control is given back to the main function.
Step 7: printf("%d,", i); It prints the value of local variable i. So, it prints '3'.
Step 8: fun2(i); The fun2(i) increements the given value of variable i prints it. Here fun2(i) becomes fun2(3) hence it prints '4' then the control is given back to the main function.
Step 9: printf("%d,", i); It prints the value of local variable i. So, it prints '3'.
Hence the output is "4 3 4 3".

61. What will be the output of the program?
#include<stdio.h>
int func1(int);
int main()
{
    int k=35;
    k = func1(k=func1(k=func1(k)));
    printf("k=%d\n", k);
    return 0;
}
int func1(int k)
{
    k++;
    return k;
}

A.k=35   
B.k=36
C.k=37   
D.k=38
Answer: Option D
Explanation:Step 1: int k=35; The variable k is declared as an integer type and initialized to 35.
Step 2: k = func1(k=func1(k=func1(k))); The func1(k) increement the value of k by 1 and return it. Here the func1(k) is called 3 times. Hence it increements value of k = 35 to 38. The result is stored in the variable k = 38.
Step 3: printf("k=%d\n", k); It prints the value of variable k "38".

62.What will be the output of the program?
#include<stdio.h>
int addmult(int ii, int jj)
{
    int kk, ll;
    kk = ii + jj;
    ll = ii * jj;
    return (kk, ll);
}
int main()
{
    int i=3, j=4, k, l;
    k = addmult(i, j);
    l = addmult(i, j);
    printf("%d, %d\n", k, l);
    return 0;
}

A.12, 12
B.7, 7
C.7, 12   
D.12, 7

Answer: Option A
Explanation:Step 1: int i=3, j=4, k, l; The variables i, j, k, l are declared as an integer type and variable i, j are initialized to 3, 4 respectively.
The function addmult(i, j); accept 2 integer parameters.
Step 2: k = addmult(i, j); becomes k = addmult(3, 4)
In the function addmult(). The variable kk, ll are declared as an integer type int kk, ll;
kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.
ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.
return (kk, ll); It returns the value of variable ll only.
The value 12 is stored in variable 'k'.
Step 3: l = addmult(i, j); becomes l = addmult(3, 4)
kk = ii + jj; becomes kk = 3 + 4 Now the kk value is '7'.
ll = ii * jj; becomes ll = 3 * 4 Now the ll value is '12'.
return (kk, ll); It returns the value of variable ll only.
The value 12 is stored in variable 'l'.
Step 4: printf("%d, %d\n", k, l); It prints the value of k and l
Hence the output is "12, 12".

63. What will be the output of the program?

#include<stdio.h>
int check(int);
int main()
{
    int i=45, c;
    c = check(i);
    printf("%d\n", c);
    return 0;
}
int check(int ch)
{
    if(ch >= 45)
        return 100;
    else
        return 10;
}

A.100
B.10
C.1   
D.0

64. If int is 2 bytes wide.What will be the output of the program?
#include <stdio.h>
void fun(char**);
int main()
{
    char *argv[] = {"ab", "cd", "ef", "gh"};
    fun(argv);
    return 0;
}
void fun(char **p)
{
    char *t;
    t = (p+= sizeof(int))[-1];
    printf("%s\n", t);
}

A.ab   
B.cd
C.ef   
D.gh
Answer: Option B
Explanation:Since C is a machine dependent language sizeof(int) may return different values.
The output for the above program will be cd in Windows (Turbo C) and gh in Linux (GCC).
To understand it better, compile and execute the above program in Windows (with Turbo C compiler) and in Linux (GCC compiler).

65.What will be the output of the program?
#include<stdio.h>
int fun(int(*)());
int main()
{
    fun(main);
    printf("Hi\n");
    return 0;
}
int fun(int (*p)())
{
    printf("Hello ");
    return 0;
}

A.Infinite loop
B.Hi
C.Hello Hi    D.
Error

Answer: Option C
 

66. What will be the output of the program?
#include<stdio.h>
int fun(int i)
{
    i++;
    return i;
}

int main()
{
    int fun(int);
    int i=3;
    fun(i=fun(fun(i)));
    printf("%d\n", i);
    return 0;
}

A.5
B.4
C.Error   
D.Garbage value

Answer: Option A
Explanation:Step 1: int fun(int); This is prototype of function fun(). It tells the compiler that the function fun() accept one integer parameter and returns an integer value.
Step 2: int i=3; The variable i is declared as an integer type and initialized to value 3.
Step 3: fun(i=fun(fun(i)));. The function fun(i) increements the value of i by 1(one) and return it.
Lets go step by step,
=> fun(i) becomes fun(3) is called and it returns 4.
=> i = fun(fun(i)) becomes i = fun(4) is called and it returns 5 and stored in variable i.(i=5)
=> fun(i=fun(fun(i))); becomes fun(5); is called and it return 6 and nowhere the return value is stored.
Step 4: printf("%d\n", i); It prints the value of variable i.(5)
Hence the output is '5'.

67. What will be the output of the program?
#include<stdio.h>
int fun(int);
int main()
{
    float k=3;
    fun(k=fun(fun(k)));
    printf("%f\n", k);
    return 0;
}
int fun(int i)
{
    i++;
    return i;
}

A.5.000000
B.3.000000
C.Garbage value
D.4.000000

Answer: Option A

68. What will be the output of the program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i=0;
    i++;
    if(i<=5)
    {
        printf("IndiaBIX");
        exit(1);
        main();
    }
    return 0;
}

A.Prints "IndiaBIX" 5 times
B.Function main() doesn't calls itself
C.Infinite loop
D.Prints "IndiaBIx"

Answer: Option D
Explanation:Step 1: int i=0; The variable i is declared as in integer type and initialized to '0'(zero).
Step 2: i++; Here variable i is increemented by 1. Hence i becomes '1'(one).
Step 3: if(i<=5) becomes if(1 <=5). Hence the if condition is satisfied and it enter into if block statements.
Step 4: printf("IndiaBIX"); It prints "IndiaBIX".
Step 5: exit(1); This exit statement terminates the program execution.
Hence the output is "IndiaBIx".