Chapter 2 SQ
Chapter 2 SQ
USER INTERACTION
1. What is operator precedence?
Definition:
“The order that determines which operator is executed first if there is more than one operator in
an expression is called operator precedence.”
When evaluating an expression containing multiple operators, C evaluated operators with higher
precedence before evaluating those with lower precedence.
2. What is ternary operator?
Definition:
“The operator that needs three operands to perform a certain operation is called ternary
operator.”
Example:
The conditional operator (? :)
3. Define binary operators.
Definition:
“The operators that need to operands to perform a certain operation are called binary
operators.”
Examples:
• All arithmetic operators
• All relational operators
• Logical OR operator (‖) • Logical AND operator (&&)
4. Define unary operator.
Definition:
“The operators that need only one operand to perform a certain operation are called unary
operators.”
Examples:
• The logical NOT operator (!)
• Unary plus operator (+)
• Unary minus operator (-)
5. How does C language perform short circuit evaluation?
• While evaluating an AND operator, if the sub-expression of the left side of the operator is false, then the
whole expression is declared as false without checking the complete expression.
• While evaluating an OR operator, if the sub expression at the left side of the operator is true, then the
whole expression is declared as true without checking the complete expression.
6. What is short circuit evaluation?
Definition:
“The process in which we deduce the result of an operation without computing whole expression
is called short circuit evaluation.”
7. What are operators in C?
Definition:
“The symbols that are used to perform certain operators on data are called operators.”
The operators in C can be categorized into the following types:
• Assignment operators • Arithmetic operators
• Relational operators • Logical operators
8. Differentiate between assignment operator and equal to operator.
Assignment Operator Equal to Operator
• It is used to assign the value to a variable • It is used to check whether the two
or a variable to another variable. given operands are equal or not.
• While using it, constant or expression • While using it, constant or expression
cannot be placed on left-hand side. E.g., can be used on the left-hand side. E.g.,
1=x; is invalid. 1= =1 and a+b= =5 is valid.
9. What are logical operators?
Definition:
“The operators that are used to combine two or more conditions (relational expressions) are
called logical operators.”
They result in true or false.
The true is represented by 1 while false is represented by 0.
There are three types of logical operators in C.
Description Operators
AND &&
OR ‖
NOT !
10. What is the use of logical NOT operator?
Definition:
“The operator that indicates or reverses the result of a condition is called NOT operator.”
Representation:
The operator is represented by the “!” sign.
Truth Table:
The truth table of logical NOT operator is as follows:
Expression Result
False True
True False
11. What is the use of logical AND operator?
Definition:
“The operator that takes two operands as input and produces a result true if both operands are
true is called AND operator.”
Representation:
It is represented by “&” symbols.
Truth Table:
Following is a truth table of AND operator:
Expression Result
False && False False
False && True False
True && False False
True && True True
12. What is the use of logical OR operator?
Definition:
“The operator that takes two operands as input and produces a result true if any of the operands
is true and returns false if both operands are false is called OR operator.”
Representation:
It is represented by ‖ symbols.
Truth table:
Following is a truth table for the OR operator:
Expression Result
False ‖ False False
False ‖ True True
True ‖ False True
13. Define relational expression and relational operators.
Relational Expression:
“The expression in which only relational operators are used is called relational expression.”
Example:
a<=b is a relational expression.
Relational operators:
“The operators that compare two values and determine the relationship between them are called
relational operators.”
Relational operators always result in Boolean expression i.e., true, or false. The true is represented by 1
and the false is represented by 0. These operators allow the user to compare numeric and char values.
Examples:
• < • <= • >
• >= • == • !=
14. What is the use of “! =” operator?
This operator returns “true ” if values on both sides of != are not equal. Otherwise Returns “false”. The
operation is called “not equal to ”.
Example:
3! =5 will result in true because value on both sides of! = signs are not equal.
15. What is the use of “= = ” operator?
This operator Returns “true ” if values on both sides of = = are equal. otherwise Returns “false ”. This
operation is called “is equal to ”.
Example:
3 = = 5 will result in false because value on both sides of = = are not equal.
16. What is the use of “> = ” operator?
This operator Returns “ true ” if the value on the left side of >= is greater than or equal to the value on
the right side. Otherwise Returns “ false ”. This operation is called “ greater than or equal to ”.
Example:
5>=5 will result in true because the value on the left side of >= is equals to the value on the right side.
17. What is the use of “> ” operator?
This operator Returns “ true ” if the value on the left side of> is greater than the value on the right side.
otherwise Returns “ false ”. This operation is called “ greater than ”.
Example:
5>4 will result in true because the value on the left side of> is greater than the value on the right side.
18. What is the use of “<= ” operator?
This operator Returns “true ” if the value on the left side of<= is less than or equal to the value on the
right side. Otherwise Returns “false ”. This operation is called “ less than or equal to ”.
Example:
5<=5 will result in true because the value on the left side of<= is equal to the right side.
19. What is the use of “< ” operator?
This operator Returns “true ” if the value on the left side of< is less than the value on the right side.
Otherwise Returns “false ”. This operation is called “ less than ”.
Example:
3<5 will result in true because the value on the left side of< is less than the value on the right side.
20. What is arithmetic expression?
Definition:
“The expression in which only arithmetic operators are used is called arithmetic expression.”
Example:
sum = a + b/c *ds
The arithmetic operators used in C are follows:
• Addition
• Subtraction
• Multiplication
• Division
• Modulus
21. What is an addition operator?
The operator is used to perform the addition of two values.
It is represented by + sign.
It belongs to the category of binary operators.
Example:
2 + 3 will result in 5.
22. What is subtraction operator?
The operator is used to perform the subtraction of the second value from the first value.
It is represented by - sign.
It belongs to the category of binary operators.
Example:
3 - 1 will result in 2.
23. What is multiplication operator?
The operator is used to perform the multiplication of two values.
It is represented by * sign.
It belongs to the category of binary operators.
Example:
2 * 3 will result in 6.
24. What is division operator?
The operator is used to perform the division of two values.
It is represented by /.
It belongs to the category of binary operators.
Example:
6 / 3 will result in 2.
25. What is modulus operator?
The operator is used to return the remainder of the division of values.
It is represented by a % sign.
It belongs to the category of binary operators.
Example:
5 % 3 will result in 2.
26. What is the statement Terminator?
In a C program, the semicolon (:) is called a statement Terminator. Each individual statement must end
with a semicolon. It indicates the end of one statement.
Example:
printf (" Hello world"); in this example as, mentioned semicolon is statement Terminator.
27. What is the purpose of getch function in C language?
This function is used to read a single character from the user. The entered character by the user is not
displayed on the output screen. This function is used to hold the execution of the program until the user
passes any key from the keyboard. This function does not take any argument. It is declared conio.h
header file.
28. What are the elements of scanf function?
There are two main parts of the scanf function:
• The first part inside the double quotes is the list of format specifiers.
• The second part is optional variables with & sign at their left.
29. What is the purpose of & sign in scanf function?
The & sign or ampersand sign is called address of operator. It is used to specify the memory location of
the variable in which the input is being stored. In case we miss this sign, then the scanf function cannot
store input in the given variable successfully.
30. What is the use of "scanf" function? Write its syntax.
This function is used to take input from the user into the variable. While reading input from the
keyboard it waits for the enter key after each character. This function is declared in <stdio.h> header
file.
Syntax:
scanf (“format specifier", &variable list);
31. What is the purpose of \t escape sequence?
It is used to add tab spaces i.e., 8 spaces on the screen.
Example:
Consider the following statement:
printf ("Sohaib \t Saleem");
The above statement will first print the word Sohaib and then \t will add tab spaces. After that, the word
Saleem will be printed.
32. What is the purpose of \n escape sequence?
It is used to move the cursor to the next line.
Example:
Consider the following statement:
printf ("Shahid\nMalik”);
The above statement will first print the word Shahid and then \n will move the cursor to the start of the
new line. After that, the word Malik will be printed.
33. How is an escape sequence formed?
Escape sequences consist of two characters. For all sequences, the first character will be “\” i.e.,
backslash.
Following are some commonly used escape sequences:
• \n • \b • \a • \t • \’ • \\
34. Define %c format specifier.
The %c specifier is used to display the value of character data type. It means it is used for printing a
character on the screen.
35. Define %f format specifier.
The %f format specifier is used to display the value of load data type. If we want to specify the precision
according to our requirement, we can write it as %nf where n number of digits.
36. Which symbol Is used to start a format specifier?
The format specifier begins with the backslash sign i.e., “\". This sign is followed by a special character
for identifying the type of data.