Question 1
#include "stdio.h"
int main()
{
struct {int a[2];} arr[] = {{1},{2}};
printf("%d %d %d %d",arr[0].a[0],arr[0].a[1],arr[1].a[0],arr[1].a[1]);
return 0;
}
Question 2
#include "stdio.h"
int main()
{
struct {int a[2], b;} arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};
printf("%d %d %d and",arr[0].a[0],arr[0].a[1],arr[0].b);
printf("%d %d %d\\n",arr[1].a[0],arr[1].a[1],arr[1].b);
return 0;
}
Question 3
Pick the best statement for the below program:
#include <stdio.h>
int main()
{
struct {
int i;
char c;
} myVar = {.i = 100, .c = 'A'};
printf("%d %c", myVar.i, myVar.c);
return 0;
}
Compile error because struct type (containing two fields of dissimilar type i.e. an int and a char) has been mentioned along with definition of myVar of that struct type.
Compile error because of incorrect syntax of initialization of myVar. Basically, member of operator (i.e. dot .) has been used without myVar.
Compile error for not only B but for incorrect order of fields in myVar i.e. field c has been initialized first and then field i has been initialized.
No compile error and it’ll print 100 A.
Question 4
Pick the best statement for the below program:
#include <stdio.h>
int main()
{
union {
int i1;
int i2;
} myVar = {.i2 = 100};
printf("%d %d", myVar.i1, myVar.i2);
return 0;
}
Compile error due to incorrect syntax of initialization.
No compile error and it’ll print “0 100”.
No compile error and it’ll print “100 100”.
Question 5
Pick the best statement for the below program snippet:
struct {int a[2];} arr[] = {1,2};
No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[1].a[0] would be 2.
No compile error and it’ll create array arr of 2 elements. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2. The second element arr[1] would be ZERO i.e. arr[1].a[0] and arr[1].a[1] would be 0.
No compile error and it’ll create array arr of 1 element. Each of the element of arr contain a struct field of int array of 2 elements. arr[0]. a[0] would be 1 and arr[0].a[1] would be 2.
None of the above
Question 6
Consider the code fragment below :
#include <iostream>
using namespace std;
void f(int n) {
if (n <= 1) {
cout << n;
} else {
f(n / 2);
cout << n % 2;
}
}
#include <stdio.h>
void f(int n) {
if (n <= 1) {
printf("%d", n);
} else {
f(n / 2);
printf("%d", n % 2);
}
}
void f (int n)
{
if (n <=1) {
System.out.print(n);
}
else {
f (n/2);
System.out.print(n%2);
}
}
def f(n):
if n <= 1:
print(n, end='')
else:
f(n // 2)
print(n % 2, end='')
function f(n) {
if (n <= 1) {
process.stdout.write(n.toString());
} else {
f(Math.floor(n / 2));
process.stdout.write((n % 2).toString());
}
}
What does f(173) print?
010110101
010101101
10110101
10101101
Question 7
Consider the code fragment written in C below :
#include <iostream>
using namespace std;
void f(int n) {
if (n <= 1) {
cout << n;
} else {
f(n / 2);
cout << n % 2;
}
}
void f (int n)
{
if (n <= 1) {
printf ("%d", n);
}
else {
f (n/2);
printf ("%d", n%2);
}
}
public class Main {
public static void f(int n) {
if (n <= 1) {
System.out.print(n);
} else {
f(n / 2);
System.out.print(n % 2);
}
}
}
def f(n):
if n <= 1:
print(n, end='')
else:
f(n // 2)
print(n % 2, end='')
function f(n) {
if (n <= 1) {
process.stdout.write(n.toString());
} else {
f(Math.floor(n / 2));
process.stdout.write((n % 2).toString());
}
}
Which of the following implementations will produce the same output for f(173) as the above code? P1
void f (int n)
{
if (n/2) {
f(n/2);
}
printf ("%d", n%2);
}
P2
void f (int n)
{
if (n <=1) {
printf ("%d", n);
}
else {
printf ("%d", n%2);
f (n/2);
}
}
Both P1 and P2
P2 only
P1 only
Neither P1 nor P2
There are 7 questions to complete.