Ternary Operator MCQ Quiz in বাংলা - Objective Question with Answer for Ternary Operator - বিনামূল্যে ডাউনলোড করুন [PDF]
Last updated on Mar 8, 2025
Latest Ternary Operator MCQ Objective Questions
Top Ternary Operator MCQ Objective Questions
Ternary Operator Question 1:
What will be the output of the following program?
#include
int main()
{
int a = 4, b = 3;
printf("%d",(a++) +b);
printf("%d",(a++) +b);
return 0;
}
Answer (Detailed Solution Below)
Ternary Operator Question 1 Detailed Solution
Concept:-
- C provides two unusual operators for incrementing and decrementing the variables
- The increment operator ++ adds 1 to its operand, while the decrement operator – subtracts 1
- The unusual aspect is the ++ and – can be used either as prefix operator or postfix operator.
Calculation:-
In the first expression:
a++ +b → a is incremented and added to b , But since a++ is post increment operation a will change its value from 4 to 5 in next instruction.
a++ +b = 4 + 3 = 7 ( and a is incremented to 5 after this)
In the next print statement
a++ +b
a = 5
b = 3
a++ +b = 8 (a is incremented to 6 after this)
Ternary Operator Question 2:
Concatenation operator in SQL* plus –
Answer (Detailed Solution Below)
Ternary Operator Question 2 Detailed Solution
Concatenation operator || Concatenate columns or character strings to other columns is SQL.
eg. SELECT ‘Name’ || enamename FROM emp;
Output will be:
Name = JONES
Name = SMITH
So, one can merge the output of 2 or more columns together using concatenation operator ||.Ternary Operator Question 3:
Which operator is used for bitwise OR in C?
Answer (Detailed Solution Below)
Ternary Operator Question 3 Detailed Solution
In C programming, the bitwise OR operator is: | (single vertical bar)
It performs a bit-by-bit OR operation between two integer operands.
🔍 Example:
int a = 5; / Binary: 0101
int b = 3; / Binary: 0011
int result = a | b; / Result: 0111 = 7
Ternary Operator Question 4:
Which of the following is a logical bitwise operator ?
Answer (Detailed Solution Below)
Ternary Operator Question 4 Detailed Solution
The correct answer is &.
Key Points
- & is a bitwise AND operator in many programming languages, including C, C++, Java, and Python.
- The bitwise AND operator performs a binary AND operation on two bits or binary numbers, returning a result where each bit is set to 1 if both corresponding bits of the operands are 1, and to 0 otherwise.
- It is used to manipulate individual bits of data, useful in low-level programming, such as system programming or embedded systems.
- Common applications include bit masking, where specific bits of a number are isolated or altered, and setting, clearing, or toggling individual bits.
Additional Information
- Bitwise Operators in General
- Bitwise operators are used to perform operations on individual bits of data.
- Common bitwise operators include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>).
- These operators are essential in various applications, such as cryptography, network programming, and error detection algorithms.
- Bit Masking
- Bit masking is a technique used to extract or manipulate specific bits within a binary number.
- A mask is created by setting specific bits to 1 and performing a bitwise AND operation with the target number.
- This technique is commonly used in embedded systems and hardware programming to control individual bits of registers.
- Bitwise Shift Operations
- Bitwise shift operations, such as left shift (<<) and right shift (>>), move bits to the left or right within a binary number.
- Left shifting a binary number by one position is equivalent to multiplying it by 2, while right shifting is equivalent to dividing it by 2.
- These operations are efficient and commonly used in algorithms requiring fast multiplication or division by powers of 2.
- Logical vs. Bitwise Operators
- Logical operators (&&, ||, !) operate on boolean values and return boolean results.
- Bitwise operators operate on integer values at the binary level and return integer results.
- Understanding the distinction between these operators is crucial for writing efficient and correct code, especially in system-level programming.
Ternary Operator Question 5:
Which operator has right to left associativity?
I. *=
II. ?:Answer (Detailed Solution Below)
Ternary Operator Question 5 Detailed Solution
- Ternary operator and assignment operator associativity is right to left
- Ternary operator is ?: and Assignment operator is *=
Example:
Ternary operator:
(condition) ? condition_if_true : condition_if false;
Assignment operator:
float a *= b;Ternary Operator Question 6:
Which of the following represents a way to multiply a number by 9?
I. int main()
{
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
printf(“%d”, (n<<3)+n);
return 0;
}
II. int main()
{
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
printf(“%d”, (n × 9));
return 0;
}
III. int main()
{
int n;
printf(“Enter a number:”);
scanf(“%d”,&n);
printf(“%d”, (n<<9));
return 0;
}Answer (Detailed Solution Below)
Ternary Operator Question 6 Detailed Solution
In Binary Left Shift Operator, the left operands value is moved left by the number of bits specified by the right operand i.e. x<
Here 2y < 9
y = 3
Consider n = 4
n ≪ 3 = 4 × 23 = 32 → 32 + n = 32 + 4 = 36 i.e. 4 × 9 = 36
Note:- For better Understanding Try to run the above program.
Ternary Operator Question 7:
What does the following program do when the input is unsigned 16-bit integer?
#include
main( )
{
unsigned int num;
int i;
scanf (“%u”, &num);
for ( i = 0; i<16; i++)
{
printf (“%d”, (num << i & 1 << 15 ) ? 1:0);
}
}
Answer (Detailed Solution Below)
Ternary Operator Question 7 Detailed Solution
Using << (Left Shift Operation) bit Pattern of the data can be shifted by specified number of positions to left. When data is shifted left , trailing zero’s are filled with zero.
Also << has higher precedence than &.
Hence given code will print binary equivalent of the given number.
Ternary Operator Question 8:
main ( )
{
int y = 1 ;
int x = y > 0 ? 5 : 10;
}
value of the x is
Answer (Detailed Solution Below)
Ternary Operator Question 8 Detailed Solution
if(a > b) {value = x;}
else{ value = y;}
can be return as:
value = a > b ? x : y
?: is called ternary operator.
Ternary Operator Question 9:
Let “if x then y else z” denote the expression and this expression can be represented as “?xyz” using ternary prefix operator.
The prefix operator for the following expression
If a then if c-d then a+c else a*c else a+b
Answer (Detailed Solution Below)
Ternary Operator Question 9 Detailed Solution
If a then if c-d then a+c else a*c else a+b
If a then ? (c-d) (a+c) (a*c) else a+b
If a then i?- cd + ac *ac else a+b
?a( ? –cd +ac *ac) a+b
?a ? –cd +ac *ac +abTernary Operator Question 10:
The associativity of the ternary operator and bit wise AND is _________.
Answer (Detailed Solution Below)
Ternary Operator Question 10 Detailed Solution
In programming languages, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses.
Precedence and associativity of operators
Operators |
Associativity |
() [] -> . |
Left to right |
! ~ ++ -- + - * & (type) sizeof |
Right to left |
* / % |
Left to right |
+ - |
Left to right |
≪ ≫ |
Left to right |
< <= > >= |
Left to right |
== != |
Left to right |
& |
Left to right |
^ |
Left to right |
| |
Left to right |
&& |
Left to right |
|| |
Left to right |
?: |
Right to left |
= += -= *= /= %= &= ^= |= ≪= ≫= |
Right to left |
, |
Left to right |
In the above table, operators on the same line have the same precedence; rows are in order of decreasing precedence.