1.
What is the output?
main()
{
char val[] = {‘a’,’b’,’c’,’\n’,’c’,’\0’};
char *p, *val1, *val2;
p = &val[3];
val1 = val;
val2 = val;
printf(“%d”, ++*p + ++*val1 – 32);
}
Correct Answer
A. Non-portable pointer conversion
2.
What is the output?
main()
{
char *arr = “abc defg”, *x;
x = arr;
while( *arr != ‘\0’ )
++*arr++;
printf(“%s %s”,arr,x);
}
Correct Answer
C. Bcd!efgh
Explanation
The program initializes a character pointer 'arr' with the string "abc defg" and another character pointer 'x'. 'x' is then assigned the value of 'arr'. The program then enters a while loop which increments the value at the memory location pointed to by 'arr' and increments the pointer 'arr' until it encounters the null character '\0'. This effectively increments each character in the string by 1. After the loop, the program prints the value of 'arr' (which is now "bcd!efgh") and 'x' (which is still "abc defg"). Therefore, the output is "bcd!efgh abc defg".
3.
What is the output?
main()
{
char far *ptr1, *ptr2;
printf(“%d %d”,sizeof(ptr1),sizeof(ptr2));
}
Correct Answer
D. 4 2
Explanation
The output of the code is "4 2". The "sizeof" operator is used to determine the size of a data type in bytes. In this case, "ptr1" is declared as a far pointer, which typically requires 4 bytes to store the segment and offset information. However, the size of a pointer can vary depending on the compiler and platform. "ptr2" is declared as a regular pointer, which typically requires 2 bytes. Therefore, the output is 4 for "ptr1" and 2 for "ptr2".
4.
What is the output?
main()
{
char family[4][20] = { “jesus”, “ram” , “rahim” , “gurunanak” };
char *temp;
int count ;
temp = family[1];
family[1] = family[2]; // Lvalue required for this line
family[2] = temp; // Lvalue required for this line
for(count = 0 ;count
Correct Answer
B. Compilation error-Lvalue required
Explanation
The correct answer is "Compilation error-Lvalue required". This is because in the lines "family[1] = family[2]" and "family[2] = temp", the left-hand side of the assignment operator is an array element, which is not a valid lvalue. An lvalue is an expression that can appear on the left-hand side of an assignment. Therefore, a compilation error occurs.
5.
Int main()
{
struct Some Name
{
int size = 5;
char tips[] = "LEARN";
};
struct SomeName ss;
printf("\n %d %s",ss.size,ss.tips);
return 0;
}
Correct Answer
C. Structure member can not be initialized
Explanation
The given code defines a structure named "SomeName" inside the main() function. The structure has two members, "size" and "tips". However, the initialization of structure members inside the structure definition is not allowed. Therefore, the correct answer is "Structure member can not be initialized".
6.
Main()
{
struct xx
{
int x=3;
char name[]="hello";
};
struct xx *s=malloc(sizeof(struct xx));
printf("%d",s->x);
printf("%s",s->name);
}
Correct Answer
C. Compiler error
Explanation
The given code will result in a compiler error because the structure `xx` contains a member `name` which is an array of characters. In C, arrays cannot be initialized inside a structure declaration. Therefore, the initialization of `name` with the string "hello" will cause a compiler error.
7.
Main()
{
char *str1="abcd";
char str2[]="abcd";
printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
}
Correct Answer
A. 2 5 5
Explanation
The correct answer is 2 5 5.
In this program, the variable str1 is a pointer to a string literal "abcd", while str2 is an array of characters with a size of 5 (including the null character).
The sizeof operator returns the size of the variable or type in bytes. Since str1 is a pointer, sizeof(str1) will return the size of a pointer (usually 2 or 4 bytes depending on the system).
On the other hand, sizeof(str2) will return the size of the array, which is 5 bytes.
Lastly, sizeof("abcd") will return the size of the string literal "abcd", which is also 5 bytes.
8.
Void main()
{
int array[]={10,20,30,40};
printf("%d",-2[array]);
}
Correct Answer
C. 30
Explanation
In C programming, array indexing can be done using pointer arithmetic. In this case, the expression "-2[array]" is equivalent to "array[-2]". Since arrays are zero-indexed, this means accessing the element at index -2. However, C does not perform bounds checking, so it accesses the memory location before the start of the array. In this case, it accesses the memory location that stores the value 30. Therefore, the correct answer is 30.
9.
Main()
{
char s[ ]="man";
int i;
for(i=0;s[ i ];i++)
printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]);
}
Correct Answer
A. Mmmm
aaaa
nnnn
Explanation
The given code is a C program that prints a sequence of characters using different methods. The code initializes a character array "s" with the value "man". Then, it enters a for loop that iterates until the null character ('\0') is encountered in the array. Inside the loop, it uses different methods to print the characters.
The first printf statement uses the array index notation "s[i]" to print the character at index i, followed by three variations of pointer arithmetic to access the same character.
Therefore, the correct answer is "mmmm aaaa nnnn" as the loop iterates over the characters in the "s" array and prints them four times using different methods.
10.
What is the the output ? main()
{
int c[ ]={2.8,3.4,4,6.7,5};
int j,*p=c,*q=c;
for(j=0;j
Correct Answer
B. 2 2 2 2 2 2 3 4 6 5
11.
What is the output ? main()
{
int i=-1,j=-1,k=0,l=2,m;
m=i++&&j++&&k++||l++;
printf("%d %d %d %d %d",i,j,k,l,m);
}
Correct Answer
A. 0 0 1 3 1
Explanation
The code initializes variables i and j to -1, k to 0, and l to 2. The expression i++&&j++&&k++||l++ is evaluated. Since i is -1, the expression i++ evaluates to false and the rest of the expression is not evaluated. Therefore, i and j remain -1, k is incremented to 1, l is incremented to 3, and m is assigned the value of l (which is 3). Finally, the printf statement prints the values of i, j, k, l, and m, resulting in the output "0 0 1 3 1".
12.
What is the output ?
void main()
{
char a[]="12345\0";
int i=strlen(a);
printf("here in 3 %d\n",++i);
}
Correct Answer
D. 3 6
Explanation
The code first declares a character array "a" with the value "12345\0". The strlen() function is then used to determine the length of the string, which is 5. The variable "i" is assigned the value of 5. The printf() function is used to print "here in 3" followed by the value of "++i", which is 6. Therefore, the output would be "3 6".
13.
Which of the following doesn’t match an array
Correct Answer
C. Heterogeneity
Explanation
The term "heterogeneity" refers to the state of being diverse or composed of different elements. In the context of arrays, it means that the elements within the array are not of the same type or size. However, arrays typically consist of elements that are of the same type and size, making "heterogeneity" the option that doesn't match.
14.
Which of the following character is appended with a string
Correct Answer
D. ‘\0’
Explanation
The character '\0' is appended with a string. This is because '\0' represents the null character, which is used to indicate the end of a string in C programming. When a '\0' character is appended to a string, it signifies the termination of the string and is used to ensure that string functions can correctly identify the end of the string.
15.
What is the output ? #include
#include
int main()
{
char *str = "x";
char c = 'x';
char ary[1];
ary[0] = c;
printf("%d %d", strlen(str), strlen(ary));
return 0;
}
Correct Answer
D. 1 (undefined value)
Explanation
The output of the program is "1 (undefined value)".
The program declares a character pointer `str` and initializes it with the string "x". It also declares a character `c` and assigns it the value 'x'. Then, it declares a character array `ary` with size 1 and assigns `c` to its first element.
When the `strlen` function is called on `str`, it returns 1 because it counts the number of characters until it reaches the null terminator '\0'.
However, when the `strlen` function is called on `ary`, it causes undefined behavior because `ary` is not null-terminated. The `strlen` function will continue reading memory until it finds a null terminator, which may result in accessing memory beyond the bounds of `ary`. The output of `strlen(ary)` is therefore unpredictable and can vary.
16.
What will be the output?
void main()
{
char c=125;
c=c+10;
printf(“%d”,c);
}
Correct Answer
A. -121
Explanation
In this code, the variable 'c' is initially assigned the value 125, which is within the range of a char data type. However, when 10 is added to 'c', the resulting value exceeds the range of a char data type. This causes an overflow and the value wraps around to the minimum value of a char data type, which is -128. Therefore, the output will be -121.
17.
Void main()
{
int a[]={0,1,2,3,4};
int * p[]={a,a+1,a+2,a+3.a+4};
printf(“%d”,*(*p));
}
Correct Answer
C. 0
Explanation
The code declares an array 'a' with 5 elements and a pointer array 'p' with 5 elements. Each element of 'p' is assigned the address of the corresponding element in 'a'. The printf statement dereferences the pointer at index 0 of 'p', which is equivalent to dereferencing the pointer at the beginning of 'a'. Therefore, the output will be the value at the first element of 'a', which is 0.
18.
An array is a
Correct Answer
C. Derived datatype
Explanation
An array is a derived datatype because it is created by combining elements of a basic datatype, such as integers or characters, into a single collection. The array itself can then be used as a datatype to store multiple values of the same type in a contiguous memory space. This allows for efficient access and manipulation of the elements within the array.
19.
The array name indicates
Correct Answer
A. Const pointer
Explanation
The term "const pointer" refers to a pointer that points to a constant value. In other words, the value being pointed to cannot be modified through the pointer. This is indicated by the "const" keyword before the pointer name.
20.
The difference between a character array and string is
Correct Answer
A. Appended ‘\0’ in string
Explanation
The correct answer is "appended '\0' in string". This is because in C programming, a string is represented as a character array terminated by a null character ('\0'). This null character indicates the end of the string. On the other hand, a character array may or may not have a null character at the end. The presence of the null character in a string allows for various string manipulation functions to work correctly, while using %s format specifier to print a character array without a null character can result in unpredictable behavior.