Site icon Exam4Training

C++ Institute CLA-11-03 CLA – C Certified Associate Programmer Online Training

Question #1

What happens if you try to compile and run this program?

#include <stdio.h>

int *f();

int main (int argc, char *argv[]) {

int *p;

p = f();

printf("%d",*p);

return 0;

}

int *f() {

static v = 1;

return &v;

}

Choose the right answer:

  • A . The program outputs 1
  • B . Compilation fails
  • C . The program outputs 3
  • D . The program outputs 2
  • E . The program outputs 0

Reveal Solution Hide Solution

Correct Answer: A
A

Explanation:

The program outputs 1 because the static variable v is initialized to 1 inside the f function, and it is visible to the main function. The f function returns the address of v, which is a pointer to an int. The main function dereferences the pointer and assigns it to p, which is another pointer to an int. Then, the main function prints the value of *p, which is the same as dereferencing p again. Therefore, the output of the program is:

f() = &v p = f() printf(“%d”,*p) = &v = 1

The other options are incorrect because they either do not match the output of the program or do not use the correct concept of static variables.

Question #2

What happens if you try to compile and run this program?

#define ALPHA 0

#define BETA ALPHA-1

#define GAMMA 1

#define dELTA ALPHA-BETA-GAMMA

#include <stdio.h>

int main(int argc, char *argv[]) {

printf ("%d", DELTA);

return 0;

Choose the right answer:

  • A . The program outputs 2
  • B . The program outputs -2
  • C . The program outputs 1
  • D . Compilation fails
  • E . The program outputs -1

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

Let’s analyze the macros and the program:

Question #2

What happens if you try to compile and run this program?

#define ALPHA 0

#define BETA ALPHA-1

#define GAMMA 1

#define dELTA ALPHA-BETA-GAMMA

#include <stdio.h>

int main(int argc, char *argv[]) {

printf ("%d", DELTA);

return 0;

Choose the right answer:

  • A . The program outputs 2
  • B . The program outputs -2
  • C . The program outputs 1
  • D . Compilation fails
  • E . The program outputs -1

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

Let’s analyze the macros and the program:

Question #2

What happens if you try to compile and run this program?

#define ALPHA 0

#define BETA ALPHA-1

#define GAMMA 1

#define dELTA ALPHA-BETA-GAMMA

#include <stdio.h>

int main(int argc, char *argv[]) {

printf ("%d", DELTA);

return 0;

Choose the right answer:

  • A . The program outputs 2
  • B . The program outputs -2
  • C . The program outputs 1
  • D . Compilation fails
  • E . The program outputs -1

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

Let’s analyze the macros and the program:

Question #2

What happens if you try to compile and run this program?

#define ALPHA 0

#define BETA ALPHA-1

#define GAMMA 1

#define dELTA ALPHA-BETA-GAMMA

#include <stdio.h>

int main(int argc, char *argv[]) {

printf ("%d", DELTA);

return 0;

Choose the right answer:

  • A . The program outputs 2
  • B . The program outputs -2
  • C . The program outputs 1
  • D . Compilation fails
  • E . The program outputs -1

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

Let’s analyze the macros and the program:

Question #2

What happens if you try to compile and run this program?

#define ALPHA 0

#define BETA ALPHA-1

#define GAMMA 1

#define dELTA ALPHA-BETA-GAMMA

#include <stdio.h>

int main(int argc, char *argv[]) {

printf ("%d", DELTA);

return 0;

Choose the right answer:

  • A . The program outputs 2
  • B . The program outputs -2
  • C . The program outputs 1
  • D . Compilation fails
  • E . The program outputs -1

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

Let’s analyze the macros and the program:

Question #2

What happens if you try to compile and run this program?

#define ALPHA 0

#define BETA ALPHA-1

#define GAMMA 1

#define dELTA ALPHA-BETA-GAMMA

#include <stdio.h>

int main(int argc, char *argv[]) {

printf ("%d", DELTA);

return 0;

Choose the right answer:

  • A . The program outputs 2
  • B . The program outputs -2
  • C . The program outputs 1
  • D . Compilation fails
  • E . The program outputs -1

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

Let’s analyze the macros and the program:

Question #8

What happens if you try to compile and run this program?

#include <stdio.h>

int main (int argc, char *argv[]) {

int i =2, j = 1;

if(i / j)

j += j;

else

i += i;

printf("%d",i + j);

return 0;

}

Choose the right answer:

  • A . The program outputs 1
  • B . The program outputs 5
  • C . The program outputs 3
  • D . Compilation fails
  • E . The program outputs 4

Reveal Solution Hide Solution

Correct Answer: E
E

Explanation:

In the if statement, i / j is 2 / 1, which is true. Therefore, the if block is executed, and j += j; doubles the value of j (j becomes 2).

After the if-else statement, printf("%d", i + j); prints the sum of i and the updated val-ue of j (2 + 2), which is 4.

Question #9

What happens if you try to compile and run this program?

#include <stdio.h>

#include <string.h>

struct STR {

int i;

char c[20];

float f;

};

int main (int argc, char *argv[]) {

struct STR str = { 1, "Hello", 3 };

printf("%d", str.i + strlen(str.c));

return 0;

}

Choose the right answer:

  • A . The program outputs 4
  • B . The program outputs 1
  • C . The program outputs 5
  • D . The program outputs 6
  • E . Compilation fails

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

The program defines a structure named STR that contains three members: an int, a char array, and a float. Then it creates a variable of type struct STR named str and initializes its members with the values 1, “Hello”, and 3. The program then prints the value of str.i + strlen(str.c), which is the sum of the int member and the length of the char array member. The length of the string “Hello” is 5, so the expression evaluates to 1 + 5 = 6. Therefore, the program outputs 6. References = C struct (Structures) – Programiz, C Structures (structs) – W3Schools, C Structures – GeeksforGeeks

Question #10

What happens if you try to compile and run this program?

#include <stdio.h>

int main (int argc, char *argv[]) {

char *p = "John" " " "Bean";

printf("[%s]", p) ;

return 0;

}

Choose the right answer:

  • A . The program outputs "[]"
  • B . The program outputs nothing
  • C . The program outputs [John Bean]
  • D . The program outputs three lines of text
  • E . The program outputs two lines of text

Reveal Solution Hide Solution

Correct Answer: C
C

Explanation:

The string literal "John" " " "Bean" is effectively concatenated into a single string by the compiler during compilation. Therefore, the value of p becomes a pointer to the string "John Bean". The printf statement then prints the string enclosed within square brackets, resulting in the output [John Bean].

Question #11

Exams Prep

What happens if you try to compile and run this program?

#include <stdio.h>

int main (int argc, char *argv[]) {

int i = ‘A’ – ‘B’;

int j = ‘b’ – ‘a’;

printf("%d",i / j);

return 0;

}

Choose the right answer:

  • A . Execution fails
  • B . Compilation fails
  • C . The program outputs 1
  • D . The program outputs -1
  • E . The program outputs 0

Reveal Solution Hide Solution

Correct Answer: D
Question #12

What happens if you try to compile and run this program?

#include <stdio.h>

int main (int argc, char *argv[]) {

char *t = "abcdefgh";

char *p = t + 2;

int i;

p++;

p++;

printf("%d ", p[2] – p[-1]);

C++ Institute – CLA-11-03

return 0;

}

Choose the right answer:

  • A . The program outputs 3
  • B . The program outputs 4
  • C . Execution fails
  • D . The program outputs 2
  • E . Compilation fails

Reveal Solution Hide Solution

Correct Answer: A
A

Explanation:

The program outputs 3 because the expression p[2] – p[-1] evaluates to 3 using the pointer arithmetic rules in

C. The pointer t points to the first element of the string literal "abcdefgh", which is stored in a read-only memory location. The pointer p is initialized to t + 2, which means it points to the third element of the string, which is ‘c’. Then, p is incremented twice, so it points to the fifth ele-ment of the string, which is ‘e’. The subscript operator [] is equivalent to adding an offset to the pointer and dereferencing it, so p[2] is the same as *(p + 2), which is ‘g’, and p[-1] is the same as *(p – 1), which is ‘d’. The printf function then prints the difference between the ASCII values of ‘g’ and ‘d’, which is 103 – 100 = 3, as a decimal integer using the %d format specifier.

References = CLA C C Certified Associate Programmer Certification, C Essentials 2 – (Intermediate), C Pointers, C Strings

Question #13

What happens if you try to compile and run this program?

#include <stdio.h>

int main(int argc, char *argv[]) {

int i = 10 – 2 / 5 * 10 / 2 – 1;

printf("%d",i);

return 0;

}

Choose the right answer:

  • A . The program outputs 0
  • B . The program outputs 4
  • C . Compilation fails
  • D . The program outputs 9
  • E . The program outputs 15

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

The expression 10 – 2 / 5 * 10 / 2 – 1 is evaluated based on the standard precedence rules in

C. Division and multiplication have higher precedence than addition and subtrac-tion, and they are evaluated from left to right:

Question #13

What happens if you try to compile and run this program?

#include <stdio.h>

int main(int argc, char *argv[]) {

int i = 10 – 2 / 5 * 10 / 2 – 1;

printf("%d",i);

return 0;

}

Choose the right answer:

  • A . The program outputs 0
  • B . The program outputs 4
  • C . Compilation fails
  • D . The program outputs 9
  • E . The program outputs 15

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

The expression 10 – 2 / 5 * 10 / 2 – 1 is evaluated based on the standard precedence rules in

C. Division and multiplication have higher precedence than addition and subtrac-tion, and they are evaluated from left to right:

Question #13

What happens if you try to compile and run this program?

#include <stdio.h>

int main(int argc, char *argv[]) {

int i = 10 – 2 / 5 * 10 / 2 – 1;

printf("%d",i);

return 0;

}

Choose the right answer:

  • A . The program outputs 0
  • B . The program outputs 4
  • C . Compilation fails
  • D . The program outputs 9
  • E . The program outputs 15

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

The expression 10 – 2 / 5 * 10 / 2 – 1 is evaluated based on the standard precedence rules in

C. Division and multiplication have higher precedence than addition and subtrac-tion, and they are evaluated from left to right:

Question #13

What happens if you try to compile and run this program?

#include <stdio.h>

int main(int argc, char *argv[]) {

int i = 10 – 2 / 5 * 10 / 2 – 1;

printf("%d",i);

return 0;

}

Choose the right answer:

  • A . The program outputs 0
  • B . The program outputs 4
  • C . Compilation fails
  • D . The program outputs 9
  • E . The program outputs 15

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

The expression 10 – 2 / 5 * 10 / 2 – 1 is evaluated based on the standard precedence rules in

C. Division and multiplication have higher precedence than addition and subtrac-tion, and they are evaluated from left to right:

Question #13

What happens if you try to compile and run this program?

#include <stdio.h>

int main(int argc, char *argv[]) {

int i = 10 – 2 / 5 * 10 / 2 – 1;

printf("%d",i);

return 0;

}

Choose the right answer:

  • A . The program outputs 0
  • B . The program outputs 4
  • C . Compilation fails
  • D . The program outputs 9
  • E . The program outputs 15

Reveal Solution Hide Solution

Correct Answer: D
D

Explanation:

The expression 10 – 2 / 5 * 10 / 2 – 1 is evaluated based on the standard precedence rules in

C. Division and multiplication have higher precedence than addition and subtrac-tion, and they are evaluated from left to right:

Question #18

Assume that ints are 32-bit wide.

What happens if you try to compile and run this program?

#include <stdio.h>

typedef union {

int i;

int j;

int k;

} uni;

int main (int argc, char *argv[]) {

uni s;

s.i = 3;

s.j = 2;

s.k = 1;

printf("%d",s.k * (s.i – s.j));

return 0;

}

Choose the right answer:

  • A . The program outputs 9
  • B . The program outputs 0
  • C . Execution fails
  • D . Compilation fails
  • E . The program outputs 3

Reveal Solution Hide Solution

Correct Answer: B
B

Explanation:

The program defines a union named uni with three members: i, j, and k. The members share the same memory location. The values are assigned to s.i, s.j, and s.k, but since they share the same memory, the value of s.i will overwrite the values of s.j and s.k.

So, s.i will be 3, and both s.j and s.k will be 3. Then, the expression s.k * (s.i – s.j) be-comes 3 * (3 – 3), which equals 0. The printf statement prints the result, and the pro-gram outputs 0.

The program is a valid C program that can be compiled and run without errors. The program defines a union type named uni that contains three int members: i, j, and k. Then it creates a variable of type uni named s and assigns values to its members. However, since a union can only hold one member value at a time, the last assignment (s.k = 1) overwrites the previous values of s.i and s.j. Therefore, all the members of s have the same value of 1. The program then prints the value of s.k * (s.i – s.j), which is 1 * (1 – 1) = 0. Therefore, the program outputs 0. References = C Unions – GeeksforGeeks, C Unions (With Examples) – Programiz, C – Unions – Online Tutorials Library

Question #19

What happens if you try to compile and run this program?

enum { A, B, C, D, E, F };

#include <stdio.h>

int main (int argc, char *argv[]) {

printf ("%d", B + D + F);

return 0;

}

Choose the right answer:

  • A . The program outputs 10
  • B . The program outputs 7
  • C . The program outputs 8
  • D . Compilation fails
  • E . The progham outputs 9

Reveal Solution Hide Solution

Correct Answer: E
E

Explanation:

The program outputs 9 because the expression B + D + F evaluates to 9 using the enumeration constants defined by the enum keyword. The enum keyword creates a user-defined data type that can have one of a set of named values. By default, the first value is assigned 0, and each subsequent val-ue is assigned one more than the previous one, unless explicitly specified. Therefore, in this pro-gram, A is 0, B is 1, C is 2, D is 3, E is 4, and F is 5. The printf function then prints the sum of B, D, and F, which is 1 + 3 + 5 = 9, as a decimal integer using the %d format specifier.

References = CLA C C Certified Associate Programmer Certification, [C Essentials 2 – (Intermediate)], C Enumeration

Exit mobile version