C Programming Interview / Viva Q&A: 4 (Logical NOT Operator)

In this video tutorial lets put our understanding of logical NOT(!) operator to test.

Related Read:
Logical Operators In C
Relational Operators In C

Logical NOT(!) Operator in C Program

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30, c;

    if( !(a > b) )
        b = 50;
    c = 70;

    printf("b = %d and c = %d\n", b, c);

    return 0;
}

In this c source code, a is 20 and b is 30. Inside if condition we check if a is greater than b. But a is less than b, so it returns false(0). But we’ve a Logical NOT operator(!) surrounding the false result. So it converts it to true. Hence the next line of code that is, b = 50 gets executed.

So the output is: b = 50 and c = 70

C Programming Interview / Viva Q&A: 4 (Logical NOT Operator)


[youtube https://www.youtube.com/watch?v=gRl9-zZMqm8]

YouTube Link: https://www.youtube.com/watch?v=gRl9-zZMqm8 [Watch the Video In Full Screen.]


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Programming Interview / Viva Q&A: 3 (Logical Operator)

In this C program we’ve 2 operands and one AND operator(&&). We use Logical NOT Operator to change the true and false conditions inside if statement. Check the source code below and guess the output.

Related Read:
Logical Operators In C

Logical && Operator in C Program

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30;

    if( a && b )
    {
        printf("a = %d\n", a);
    }
    else
    {
        printf("b = %d\n", b);
    }
    return 0;
}

Output:
a = 20

In above C program, a = 20 and b = 30. In C, any non-zero number is considered as true. So the condition is if statement returns true and the block of code inside if statement gets executed.

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int a = 20, b = 30;

    if(  !(!a) && a  )
    {
        printf("a = %d\n", a);
    }
    else
    {
        printf("b = %d\n", b);
    }
    return 0;
}

Lets evaluate the if condition in above c source code. a = 20, which is non-zero number, so its considered as true(1). So negating the true(1) value !a gives false(0). Negating that false(0) value !(!a) gives true(1).

So !(!a) returns true(1). So both sides of &&(AND) is true, so the whole thing returns true. So the block of code inside if condition gets executed.

Output:
a = 20

C Programming Interview / Viva Q&A: 3 (Logical Operator)


[youtube https://www.youtube.com/watch?v=JxPB_JxNyHY]

YouTube Link: https://www.youtube.com/watch?v=JxPB_JxNyHY [Watch the Video In Full Screen.]


Important:
1. Logical &&(AND) Operator returns true when both sides operands have true(non-zero number) value. In any other case it returns false(0) value.
2. Any non-zero number is considered as true and zero(0) is considered as false in C programming language.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Programming Interview / Viva Q&A: 2

This C program has a integer variable and 2 printf functions. Check the source code below and guess the output.

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int a;

    a = printf("IBM");

    printf("\n%d", a);

    return 0;
}

Output 1:
IBM
3

Output 2:
Microsoft
9

Output 3:
Apple
5

Output 4:
Oracle
6

C Programming Interview / Viva Q&A: 2 (printf)


[youtube https://www.youtube.com/watch?v=e9gZ9Vk8lf4]

YouTube Link: https://www.youtube.com/watch?v=e9gZ9Vk8lf4 [Watch the Video In Full Screen.]


Important: printf() function returns the number of characters printed by it.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Programming Interview / Viva Q&A: 1

Guess the output of this simple C program. You can check these 2 video tutorials listed below so that you get to know todays interview / viva question.

Related Read:
if else statement in C
Relational Operators In C

Whats the output of this C Program?

 
#include < stdio.h >

int main()
{
    int num;

    printf("Enter a number\n");
    scanf("%d", &num);

    if(num = 5)
    {
        printf("You entered number 5\n");
    }
    else
    {
        printf("You entered %d \n", num);
    }

    return 0;
}

If user enters num value as 10. What does the above C Program output?

Answer: Did you answer – You entered 10 as output? If yes, then its wrong.

Note:
1. If at first glance of the source code you didn’t get the answer right, then go through the videos I’ve posted in ‘Related Read” section above.
2. After going through the 2 videos I’ve posted in ‘Related Read’ section above, if you still didn’t get the answer, then please open your editor and type the program and execute it one line at a time.

Hint: assignment operator(=) and equality operator(==) are different and work differently in C programming.

C Programming Interview / Viva Q&A: 1


[youtube https://www.youtube.com/watch?v=Zg04m3hF69E]

YouTube Link: https://www.youtube.com/watch?v=Zg04m3hF69E [Watch the Video In Full Screen.]


Here is the Answer:

As you can see in the condition of if, it’s written as num = 5. = is assignment operator. So here 5 is being assigned to variable num, and thus it returns 5. In C program, any non-zero number is considered as true. So every time the condition in if becomes true, so the block of code present in if gets executed. i.e., value of num is 5.

using equality operator for comparison

 
#include < stdio.h >

int main()
{
    int num;

    printf("Enter a number\n");
    scanf("%d", &num);

    if(num == 5)
    {
        printf("You entered number 5\n");
    }
    else
    {
        printf("You entered %d \n", num);
    }

    return 0;
}

Output 1:
Enter a number
10
You entered 10

Output 2:
Enter a number
5
You entered number 5

Output 3:
Enter a number
1000
You entered 1000

Note: Both programs are correct syntactically. That’s the reason compiler did not throw any error. It’s a logical error, so we need to be very careful while writing programs. If we do this simple logical error in a big program, it might take a lot of time to get to know the bug and fix it. So careful. Happy coding.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert