Blog
2024.04.26.1
LTS (Link)
Apr 26, 2024
LTS (Link) tries to explain the tricks used in Information Technology pre exam files

Btw it is assumed that you know that in C, array index starts from 0, so a[0] will access the first element, a[1] the second, and so on.

PreExam_08_07_2023_B

3. What is the output of the following C program?

int incr(int i)
{
    static int count = 0;
    count = count + i;
    return count;
}

void main()
{
    int i, j;
    for (i = 1; i <= 5; i++)
    {
        j = incr(i);
    }
}

If you just learned basic C/C++, you might think that int count = 0 will keep “reset” the count variable to 0. However, it’s declared to be static, so the value will not be reset. Think of it like declaring the count variable OUTSIDE of the function.

int count = 0;

int incr(int i)
{
    count = count + i;
    return count;
}

void main()
{
    int i, j;
    for (i = 1; i <= 5; i++)
    {
        j = incr(i);
    }
}

Now it’s clear that count will be 1, 3, 6, 10, and finally 15, therefore in the end j = 15

However, even though the question asked “what is the output…”, there’s no printf or cout, so it’s unclear if he’s trying to trick us (the program doesn’t output anything), or just a typo.

4. What is the output of the following C program?

int foo(int n)
{
    if (n > 0)
        return n + foo(n - 2);
    return 0;
}

int main()
{
    int n = 10;
    print("%d", foo(10));
}

There’s no trick here, but there’s a typo (print instead of printf). The answer should be pretty obvious, it’s 10 + (8 + (6 + (4 + (2 + 0)))) = 30

9. What is the output of the following C program?

int main()
{
    int a[] = {1, 2, 3, 4};
    int sum = 0;
    for (int i = 0; i < 4; i++)
    {
        sum += a[i]
    }
    printf("%d", sum);
    return 0;
}

No tricks, just looping through the a array and adding all numbers to the sum variable. Answer should be pretty obvious, 1 + 2 + 3 + 4 = 10

10. What is the output of the following C program?

int foo(int num)
{
    if (num == 0)
        return 0;
    return (num % 2) + 10 * foo(num / 2);
}

int main()
{
    int n = 3;
    printf("%d", foo(n));
}

No tricks here, but it might not be obvious that C/C++ will “truncate” decimals values to integers. For example, if num is an integer, doing num / 2 when num = 3 will result in 1. Basically 1.5 -> 1 (notice that the value is truncated, not rounded)

% is just calculate the remainer, so 5 % 2 = 1 because 5 / 2 = 2 remainder (left over) 1.

The answer is 1 + 10 * (1 + 10 * 0) = 11

12. What will be the output of this program

int main()
{
    int a = 10, b = 20;
    a = a + b;
    b = a - b;
    a = a - b;
    printf("a=%d b=%d", a, b);
}

Just do the math lol, some of you might recognize that this is the classic swap 2 numbers algorithm.

Steps by step:

  • a = a + b = 10 + 20 = 30
  • b = a - b = 30 - 20 = 10
  • a = a - b = 30 - 10 = 20

Output should be a=20 b=10

13. What will the result of num1 variable after execution of the following statements?

int j = 1, num1 = 4;
while (j <= 10)
{
    num1 = num1 + 1;
    j = j + 1;
}

Do the math/code in your head, the answer should be 14. If you got 13 it’s probably because you miscount, notice that it’s while (j <= 10)

15. What is the output of the following C program?

int main()
{
    int arr[5] = {1, 3, 2, 3, 4};
    int freq[5] = {1, 1, 1, 1, 1};
    for (int i = 0; i < 5; i++)
    {
        for (int j = i + 1; j < 5; j++)
        {
            if (arr[i] == arr[j])
                freq[i]++;
        }
    }
    for (int i = 0; i < 5; i +=)
    {
        if (freq[i] == 1)
            printf("%d ", arr[i]);
    }
    return 0;
}

This one is hard/long to explain in written text, so buckle up.

The first for loop is looping from 0 to 5.

The second for loop is looping from i + 1 to 5.

The if statement is checking if arr[i] === arr[j], and if so, increment freq[i];

here’s what the looping looks like

ijarr[i]arr[j]arr[i] == arr[j]
01arr[0] = 1arr[1] = 3false
02arr[0] = 1arr[1] = 2false
03arr[0] = 1arr[1] = 3false
04arr[0] = 1arr[1] = 4false
12arr[1] = 3arr[2] = 2false
13arr[1] = 3arr[3] = 3true => freq[1]++

If you go through everything you will notice that only freq[1]++ so freq[1] == 2

The last for loop will loop through freq and check if freq[i] == 1 and if so print arr[i]. freq[1] == 2 so arr[1] is “skipped”.

The output should be 1 2 3 4

18. What is the result after execution of the following code if a is 10, b is 5, and c is 10?

if ((a > b) && (a <= c))
    a = a + 1;
else
    c = c + 1;

Just do the math/code lol, it should be obvious that a > b (10 > 5) and a <= c (10 <= 10), so a = a + 1 is executed.

The answer should be a = 11, c = 10

19. What is the output of the following C program?

int main()
{
    int num = 123;
    while (num != = 0)
    {
        print("%d", num);
        num = num / 10;
    }
}

Once again, we encounter a print typo and integer division (num = num / 10). The answer should be pretty obvious, it’s 123 12 1. 0 is not output because when num = 0 the while(num != 0) will exit.

20. What is the output of the following C program?

int foo(int x)
{
    if (x == 1)
        return 1;
    else
        return x * foo(x - 1);
}

int main()
{
    int n = 4;
    printf("%d", foo(n));
}

Some of you might already realize this is calculating 4! (4 factorial), if not, writing down what the code do will help. The answer looks something like 4 * (3 * (2 * 1)) = 24

PreExam_08_07_2023_C (Exam code 1911)

2. What is output of below code?

void main()
{
    char name[] = "CSEHCMUT";
    int len;
    int size;
    len = strlen(name);
    size = size_of(name);
    cout << len << size;
}

Second question already require knowledge of how C-styled string (also known as null-terminated strings) works bruh.

I have already provided you with the terminology if you want to learn more about C-styled string / null-terminated strings, but all you need to know is that

  1. strlen is short for string length, which will return the length of the string (the number of characters), in this case, 8
  2. size_of will return the “actual” size of the string. C styled strings is “terminated” with null (basically the way C knows that the string ends here.), which adds an extra byte. in this case size_of will return 9

The answer should be 8,9

3. What is output of the second cout?

int sum(int a, int b = 20)
{
    int result;
    result = a + b;
    return (result);
}
int main()
{
    int a = 100;
    int b = 200;
    int result;
    result = sum(a, b);
    cout << "Total value is :" << result << endl;
    result = sum(a);
    cout << "Total value is :" << result << endl;
    return 0;
}

If you are looking for what’s the int b = 20 called, it’s called function parameter default value. It’s a way of making that parameter optional.

For example, you might write a root function that calculate the nth root of a number which by default will calculate the square root of that number. You can write it as int root(int number; int nth_root = 2). This way, if you want to calculate the square root of 4, you can just do root(4), no need to do root(4, 2), and you calculate the cube root of 4 you can do root(4, 3)

sum(a, b): the b will basically “override” the default value, so it will return a + b;

sum(a): the default value is used, so it will return a + 20;

Answer should be 120

4. What should be the output of this program

int main(void)
{
    int a = 10 / 3;
    cout << a;
}

The terminology you’re looking for is C/C++ Integer Division. 3.(3) will be truncated to 3. The answer should be 3 (NOT 3.0 nor 3.33)

5. What is output of below program?

void main()
{
    const int a = 10;
    cout << ++a;
}

This question might trick people who know the difference between ++a and a++, but a is declared to be constant, so the answer will be Compilation Error.

In an alternate universe, if a is not constant, then the output will be 11.

In another alternate universe, if a is not constant and the cout code is cout << a++; then the output will be 10.

Basically ++a will do a = a + 1 before anything else, and a++ will do a = a + 1 after everything else.

7. What is printed to the console

void main()
{
    int a = 3;
    int b = 5;

    test_function(b, a);
}

void test_function(int a, int b)
{
    a = a + 1;
    b = b + 2;
    cout << a << "-" << b;
}

Trick question. Notice the test_function(b,a). The answer should be 6-5

11. What is printed to the console?

void main()
{
    int a = 3;
    int b = 5;
    test_function(a, b);
}
void test_function(int a, int b)
{
    a = a + 1;
    b = b + 2;
    cout << a << "-" << b;
}

Not a trick question, the answer should be 4-7

Technically the answer would be Compile Error, since in C/C++ you have to declare the function before you can use it, but the teacher I ran the code apparently.

14. What is output of this program?

int num = 2;
switch (num)
{
case 1:
    printf("I am One");
    break;
case 2:
    printf("I am Two");
    break;
case 3:
    printf("I am Three");
    break;
default:
    printf("I am not 1, 2 and 3.");
}

Not a trick question, the answer should be I am Two

I’m actually suprised he didn’t trick by not break-ing, because here’s what going to happen if there’s no break.

int num = 2;
switch (num)
{
case 1:
    printf("I am One");
case 2:
    printf("I am Two");
case 3:
    printf("I am Three");
default:
    printf("I am not 1, 2 and 3.");
}

In this alternate universe, the output will be I am Two AND I am Three AND I am not 1, 2 and 3.. This might seems obvious to people who know how C family switch case fallthrough works, but for those who don’t basically if you don’t break everything below will also execute. This behavior is intended to allow cool stuff like this

int num = 2;
switch (num)
{
case 1:
case 2:
case 3:
    printf("I am One, Two, or Three");
    break;
default:
    printf("I am not 1, 2 and 3.");
}

but it has the side effect that if you forgot to write break, extra things might run.

15. What is the value of sum printed to the console?

void main()
{
    int a[5] = {1,2,3,4,5];
    int sum = 1;
    for (int i = 0; i < 5; i++)
    {
        if (a[i] % 2 == 1)
            sum = sum + a[i]
    }
    count << sum;
}

Did you really ran the code, teacher? There are two typos here, one is count << sum instead of the usual cout, and the second one is int a[5] = {1,2,3,4,5]; (] instead of })

Anyways this code is just summing every odd element in the array (if a[i] % 2 == 1), so the answer should be 1 + 3 + 5 = 9 right? NO, BECAUSE WHO THE FUCK INITIALIZE SUM TO BE 1 (int sum = 1)? THE ANSWER IS 10 BECAUSE FUCK YOU, LET’S TRICK STUDENTS BECAUSE WHY NOT.

It’s 1AM right now, I probably should stop doing this, but I want to finish what I started.

17. What is printed to the console?

void main()
{
    int a = 3;
    int b = 5;
    swap(a, b);
    cout << a << "-" << b;
}
void swap(int m, int n)
{
    m = n;
    n = m;
}

I would have let the last question slide, but this one is the one that breaks the camel’s back and made me write this.

First, you need to know that when you “pass” variables to a function, whatever the function is doing to that variables will not change the variable outside of that function. Another way of thinking this is that the function is changing the “clones” of the variables you “pass” into it.

TL;DR the swap(a,b) does absolutely nothing, so cout << a << "-" << b; will print 3-5 right? Right?

Remember a few questions ago when I said that “Technically this code will Compilation Error, because in C/C++ you need to declare the function before you can use it”? Well, in this case we’re pretending that there’s no compilcation error. Or is it?

I was “teaching” 2 other people about the tricks that he used. After explaining everything above I went to do a little demo and WHAT THE FUCK WHY DOES IT 1. NOT ERROR AND 2. OUTPUT 5-3??? I waste like a few minutes until I realize that C/C++ has a built in swap(a, b) function that, you guessed it, actually swap a and b.

The code didn’t error because void swap(int m, int n) is basically ignored (it was declared AFTER void main()), so the swap(a,b) in void main() is using the built in swap function, which actually swap a and b.

I pretended to asked the teacher about this and he just tell me to review pass by reference/pointer, which implies that my/his code is passing variables by reference/pointer, which it doesn’t. Did he even read my/his code? DID HE EVEN RUN HIS OWN CODE???

Later, when another student ask about functions that don’t return (like int foo() {}, which when I tested just return a random number every time), it eventually devolves into him flexing his past works??? Like is he having superiority complex or something???

Like dude, RAN YOUR FUCKING CODE BEFORE PRETENDING THAT YOU KNOW EVERYTHING.

HERE, LET ME FIX THE QUESTION FOR YOU. EITHER MOVE THE SWAP FUNCTION ABOVE THE VOID MAIN FUNCTION, OR EVEN BETTER, LEARN HOW TO FUCKING NAME A FUNCTION SO THAT IT DOESN’T CONFLICT WITH THE BUILT IN ONE

18. What is printed to the console?

void main()
{
    int a = 3;
    int b = 5;

    test_function(a, a);
}

void test_function(int a, int b)
{
    a = a + 1;
    b = b + 2;
    cout << a << "-" << b;
}

At this point it’s not even a trick question anymore if you’re so used to the tricks he used. The trick here is test_function(a,a). The answer should be 4-5

19. What will be the output of the following C code?

#include <stdio.h>
int main()
{
    void foo();
    printf("1 ");
    foo();
}
void foo()
{
    print("2 ")
}

C++ function declaration isn’t scoped, so the above code can be rewritten as below

#include <stdio.h>

void foo();

int main()
{
    printf("1 ");
    foo();
}

void foo()
{
    print("2 ")
}

But wait, you might say, wouldn’t this error? The void foo(); doesn’t have any code in it?

Remember when I said you have to declare function before you can use it? Well that’s what void foo(); is, just a declaration. Later the foo() function is “implemented” below like usual.

Imagine you have a 1000 lines long foo() function. It would be really annoying that everytime you or someone else open the file they have to scroll down a lot just to see what the code actually does in the int main() function.

void foo(); allows you to tell C++ that “Hey, please don’t error when I use foo() in int main(), I promise that you will see me ‘implementing’ the code inside the foo() function later”.

Anyways the answer should be 1 2.

26.What is output of below program?

void main()
{
    int i, j, count;
    count = 0;
    for (i = 0; i < 5; i++)
    {
        for (j = 0; j < 5; j++)
        {
            count++;
        }
    }
    cout << count;
}

Just do the math or run the code inside your head lol, the answer should be 25

29. What are the values of a and b after executing this program?

void main()
{
    int a = 2;
    int b = 3;
    if (a > 1)
    {
        if (b > 3)
        {
            b = b + 1;
            a = a + 2;
        }
    }
    else
    {
        a = a + 1;
        b = b + 2;
    }
}

Just run the code inside your head, the answer should be a = 2, b = 3. Need an explanation?? Okay so

  • if (a > 1): 2 > 1 is true, so the code inside the if is run (and the code inside the else is not run.)
  • if (b > 3): 3 > 3 is false, so the code inside the if is not run. Notice that there’s no else, so like in the end nothing meaningful is run, a and b stays the same.

31. What is output of below program?

void main()
{
    int i, j;
    for (i = 0, j = 0; i < 5; i++)
    {
        cout << i, j << "--";
    }
}

In case it isn’t obvious, only i is increasing (and j stays 0), so the answer should be 00--10--20--30--40-- right? Right?

This is why I keep saying RAN YOUR FUCKING CODE DUDE, BECAUSE IF YOU WOULD HAVE YOU WOULD NOTICE THE TYPO AT cout << i,j << "--" (i,j instead of the usual i << j) AND SEE THAT C/C++ IGNORE ,j SO IT ONLY OUTPUT 0--1--2--3--4--

The answer is 00--10--20--30--40-- btw.

33. What is output of the first cout?

int sum(int a, int b = 20)
{
    int result;
    result = a + b;
    return (result);
}
int main()
{
    int a = 100;
    int b = 200;
    int result;
    result = sum(a, b);
    cout << "Total value is :" << result << endl;
    result = sum(a);
    cout << "Total value is :" << result << endl;
    return 0;
}

The answer is Total value is :300. If you don’t know why, check the explanation that I wrote for question 3. This question is asking the output of the first cout, while question 3 is asking the output of the second cout.

35. What is output of below program?

void main()
{
    int i, j, count;
    count = 0;
    for (i = 0; i < 5; i++);
    {
        for (j = 0; j < 5; j++);
        {
            count++;
        }
    }
}

This questions looks familiar… Wait isn’t this question the same as question 26? Did the teacher accidentally include the same question twice? The answer is also 25 right? Right?

If you’re good at finding the differences you will notice two extra ; at the end of the for line. Does it do anything? Yes it does because FUCK YOU LET’S TRICK STUDENTS IN THE DUMBEST WAY POSSIBLE.

Recall that there are two ways of using for loop.

for (int i = 0; i < 5; i++)
    cout << i << endl;

for (int i = 0; i < 5; i++)
{
    cout << i;
    cout << endl;
}

The first one is only possible if there’s only one statement, the second one is possible regardless whether there’s only one statements or more.

Well guess what, ; and nothing else is a empty statement! It’s a statement that does… absolutely nothing!

Let’s reformat the code so that you see what I mean

void main()
{
    int i, j, count;
    count = 0;
    for (i = 0; i < 5; i++)
        ;
    {
        for (j = 0; j < 5; j++)
            ;
        {
            count++;
        }
    }
}

Now it’s obvious that it’s actually the first way of using for loops, so the ; basically “exit” the loop and the {} is just creating a new scope. To be clear, the code inside {} is still executed, it’s just not “belonging” to the for loop, so it only run once.

Anyways the answer is 1.

36. What are printed to the screen of the below program?

int main() {
	int num;
	for(num =1 ; n < 10; num ++) {
		if(num % 3 == 1)
		cout << num;
	}
}

The answer is 1,4,7. There’s no 10 because the condition is n < 10, so when num reaches 10 the loop will exit.