Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

q.

Find the error

char s1[]=”PES”;

char s2[4];

s2=s1;

s2[0]=’M’;

printf(“%s”, s1);

Answer: The error in the code is the assignment s2 = s1. In C, arrays cannot be assigned using
the assignment operator ( =). Instead, you need to use string manipulation functions like strcpy
to copy the contents of one array into another.

q. predict output

int main(){

printf("%d", ++3);}

Answer: The expression ++3 is not valid in C. The ++ operator is used to increment the
value of a variable, but it cannot be used with a constant or a literal.

In the line printf("%d", ++3);, the code attempts to increment the value of the
constant 3. However, this is not allowed in C and will result in a compilation error.

Q. Predict output

printf("%d", -1?0:1);

The expression (-1 ? 0 : 1) is a conditional (ternary) operator in C. It evaluates a


condition and returns one of two values based on the result of the condition.

In the line printf("%d", -1 ? 0 : 1);, the condition -1 is considered true because it


is non-zero. Therefore, the expression will evaluate to the second value, which is 0.

So, the output of this code will be:0

Q. int a; printf("%d", a=0 && 2==2);


 The expression a = 0 && 2 == 2 assigns the result of the logical AND operation
to a.
 Since the logical AND ( &&) operator has a higher precedence than the assignment
(=) operator, the expression is evaluated as (a = (0 && (2 == 2))).
 Now, evaluating the logical AND part (0 && (2 == 2)), we have 0 && 1, which
results in 0 (false).
 Finally, the value 0 is assigned to a.
 The printf function then prints the value of a using the %d format specifier.:
 Output 0

Q. int a; printf("%d", a=10 | 2==2);


 Since the bitwise OR ( |) operator has lower precedence than the equality ( ==)
operator, the expression is evaluated as (a = ((10 | 2) == 2)).
 Evaluating the bitwise OR part (10 | 2), we have 10 | 2, which results in 10.
 Now, we have 10 == 2, which is 0 (false) because 10 is not equal to 2.
 Finally, the value 0 is assigned to a.
 The printf function then prints the value of a using the %d format specifier.
 Output 0

Q. WRITE PROGRAM THAT COUNTS characters and words from a line input from
user

Answer:
Q. find second largest element of an array
Q. Predict output
Int main(){
Int a[6] = {77,22,34,66,99};
Int *b=a;
Printf(“%d”, a[5]);
Printf(“%d”, ++*b);
Printf(“%d”, (*c)++);
Return 0;}

The array a is declared with a size of 6 but initialized with only 5 elements.
a[5] would lead to potential issues accessing uninitialized memory.

1. The line printf("%d\n", ++*b); increments the value pointed to by b (which is


77) by one before printing it. The result is 78.
2. The line printf("%d\n", (*c)++); prints the value pointed to by c (which is 77)
and then increments it by one. The initial value is printed as 77, and after the
increment operation, the value becomes 78.

Output: junk, 78, 77

Q. given two strings, write a program to count every letter of str1 in str2
Q. write a c program to display list of students in 6th semester. Student struct has name,
id and semester fields

Q.
Struct car

Int year;

Char company[100];

} typedef struct car car_t;

Write c program to create a pointer to structure, allocate memory and assign values of
members using the pointer

Answer:
Q. What is the output
 The struct Binary is defined with two bit fields: a with 2 bits and b with 4 bits.
 The union Ternary is defined with three members: a and b of type int and c as
a character array of size 100.
 In the main function, a variable b of type struct Binary is declared.
 The value 16 is assigned to b.b.
Since the bit field b in struct Binary is defined with 4 bits, it can store values from 0 to 15.
Assigning the value 16 to b.b may lead undefined behavior.

Offset of the member c within union Ternary. In a union, all members share the same
memory space, so the offset of each member within the union will be 0.

Q. Write output
 In the enum city, the identifiers Bangalore, Mysore, Mangalore, and Pune are
assigned values 0, 5, 6, and 7 respectively.
 The first printf statement prints the values of Mysore and Bangalore, which are
5 and 0 respectively.
 The variable c is assigned the value Pune from the enum city.
 The second printf statement performs arithmetic operations using c and
Mysore. c * Mysore evaluates to 7 * 5 = 35, and c / Mysore evaluates to 7 /
5=1

Q. Predict output

 The modify function is defined with a static variable a initialized to 10.


 Within the modify function, the current value of a is printed using printf, and
then a is decremented by 1 using the pre-decrement operator --a.
 The modify function is called twice in the main function.
 In the first call to modify, the current value of a is 10, which is printed as 10.
 In the second call to modify, the value of a is now 9 (decremented from the
previous value of 10), which is printed as 9.
Output: 10, 9

You might also like