Computer Chapter 2 Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

ALLIED SCHOOL

GROWING TOGETHER

NAME _______________ CLASS 10th


CHAPTER 1: INTRODUCTION TO PROGRAMMING
SHORT QUESTIONS
1. What is meant by computer program?
Ans. Computer need to be fed a series of instructions by humans which tell them how to perform a particular task. These
series of instructions are known as a computer program or software.
2. What is computer programming?
Ans. The process of feeding or storing instructions in the computer is known as computer programming.
3. Who is programmer?
Ans. The person who knows how to write a computer program correctly is known as computer programming.
4. What is meant by programming language?
Ans. Computers have their own special language designed by computer scientists. Programmers write computer programs in
these special languages called programming languages.
Example: C, C++, C#, Python etc.
5. Who developed C language?
Ans. C language was developed by Dennis Ritchie between 1969 and 1973 at Bell Labs.
6. What is meant by programming environment?
Ans. A collection of all the necessary tools for programming makes up a programming environment. It works as a basic
platform to write and execute programs.
7. Define IDE and give example.
Ans. A software thar provides a programming environment to facilitate programmers in writing and executing computer
programs is as an Integrated Development Environment (IDE).
Example: Xcode Visual Studio Code::Blocks Dev C++
8. Define text editor?
Ans. A text editor is a software that allows programmers to write and edit computer programs. All IDEs have their own
specific text editors. It is the main screen of an IDE where we can write our programs.
Example: Code::Blocks
9. What are the steps to execute a C language program in Code::Blocks?
Ans. Step 1. Open Code::Blocks
Step 2. Click on the file and then click new
Step 3. Write C program
Step 4. Save file with extension .c
Step 5. Click on the build and run button to see the program’s output
10. Write a program in C language to display Hello World.
Ans. #include <stdio.h>
Void main ()
{
Printf(“Hello World”);
}
11. Define compiler?
Ans. A compiler is a software that is responsible for conversion of computer program written in some high level
programming language to machine language.
12. What is the purpose of compilers?
Ans. Computers only understand and work in a machine language consisting of 0s and 1s. they require the conversion of a
program written in programming language to machine language, in order to execute it. This is achieved using a compiler.
13. Define syntax of programming language.
Ans. Each programming language has some primitive building blocks and provides some rules in order to write an accurate
program. This set of rules is known as syntax of the language. Syntax can be thought of as grammar of programming
language.
14. Define syntax error? OR What happens if we do not follow the syntax of a programming language?
Ans. If a proper syntax or rules of the programming language are not followed, the program does not get compiled. In this
case, the compiler generates error. This kind of error are called syntax are called syntax errors.
15. Define reserved words?
Ans. Every programming language has a list of words that are predefined. Each word has its specific meaning already known
to the compiler. These words are known as reserved words or keywords
Example: int, if and return are reserved words in c language.

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

16. Why reserved words cannot be used as identifier name?


Ans. If a programmer gives them a definition of his own, it causes a syntax error. Due to which reserved words cannot be
used as identifier name.
17. Write the main parts of the structure of C program?
Ans. A program can be divided into three main parts:
 Link section or header  Main section  Body of main ()
section function
18. What are header files?
Ans. While writing program in C language, we make extensive use of function that are already defined in the language. But
before using the existing functions, we need to include the files where these functions have been defined. These files are
called header files.
Syntax: #include < header file name >
Example: #include < stdio.h >
19. Define main section of C language.
Ans. It consist of main () function. Every C program must contain a main () function and it is the starting point of execution.
20. What does the body of main () function contain?
Ans. The body of main () function is enclosed in curly braces {}. All the statements inside these curly braces make the body
of main function.
Example: Void main ()
{
Printf(“Hello World”);
}
21. Write the rules to write the syntactically correct program in C.
Ans. Following points must be kept in mind to write syntactically correct program in C language:
 The sequence of statements in a C language program should be according to the sequence in which we want our
program to be executed.
 C language is case sensitive. It means that if a keyword is defined with all small case letters, we cannot capitalize
any letter i.e. int is different from Int. Former is a keyword, whereas latter not.
 Each statement ends with a semi-colon ; symbol.
22. What are comments in C?
Ans. Comments are the statements in a program that are ignored by the compiler and do not get executed. Usually comments
are written in natural language in order to provide description of our code.
Example: //This is a comment.
23. What is the purpose of writing comments?
Ans. Comments can be thought of as documentation of the program. There are two main purposes of writing statements:
 They facilitate other programmers to understand our code.
 They help us to our own code even after years of writing it.
24. Why comments are not executed?
Ans. We do not want these statements to be executed, because it may cause syntax error as the statements are written in
natural language.
25. How many types of comments are there in C language?
Ans. There are two types of comments in C language.
1. Single line comment 2. Multi line comment
26. What are single line comments?
Ans. Single line comments start with //. Anything after // on the same line, is considered a comment.
Syntax: //This is single-line comment.
27. What are multi line comments?
Ans. Multi line comments start with /* and end at */. Anything between /* and */ is considered as a comment, even on
multiple lines.
Syntax: /*This is
a multi-line
comment*/
28. What are character set?
Ans. Each language has a basic set of alphabets called character set. These are combined in an allowable manner to form
words, and then these words can be used to form sentences.
29. Write character set of C programming language.
Ans. In C programming language we have a character set that includes:
 Alphabets (A, B, C…Z), (a, b, c…z)  Digits (0-9)
 Special symbols (~ `! @ # $ % ^ & * ( ) _ + = | \ { } [ ] : ; ” ’ < > , . ? / )

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

30. What are constants?


Ans. Constants are the values that cannot be changed by a program e.g. 5, 75.7, 1500 etc. In C language, primarily we have
three types of constants:
 Integer constants  Real constants  Character constants
31. Define integer constant.
Ans. These are the values without a decimal point e.g. 7, 1256, 30100, 55555, -54, -2349 etc. They can be positive or
negative. If the value is not proceeded by a sign, it is considered as positive.
32. Define real constant.
Ans. These are the values including a decimal point e.g. 3.14, 15.3333, 75.0, -1575.76, -7941.2345 etc. they can also be
positive or negative.
33. Differentiate between integer constant and character constant.
Ans. A digit used as a character constant i.e. ‘9’ is different from a digit used as an integer constant i.e. 9.
Integer constant Character constant
We can add two integer constants to get the obvious We cannot add a character constant to another
mathematical result e.g. 9 + 8 = 17 character constant to get obvious mathematical result
e.g. ‘9’ + ‘8’ ≠ 17
34. Define variables.
Ans. A variable is actually a name given to a memory location, as the data is physically stored inside the computer’s memory.
The value of a variable can be changed in a program. Each variable has a unique name called identifier and has a data type.
35. What are identifiers?
Ans. Each variable has a unique name called identifier.
36. How can we define a variable in C language?
Ans. C language has different data types such as int, float, and char. The types int, float and char are used to store integer,
real and character data respectively.
Example: float f = 23.5;
37. What are data types?
Ans. Each variable in C language has a data type. The data type not only describes the type of data to be stored inside the
variable but also the number of bytes that the compiler needs to reserve for data storage.
Example: int, float, char
38. Define integer data type.
Ans. Integer data type is used to store integer values (whole numbers). Integer takes up to 4 bytes of memory. The keyword
int is used to declare the variable of type integer.
39. Differentiate between signed and unsigned int.
Ans. Signed int Unsigned int
A signed int can store both positive and negative values. An unsigned int can store only positive values.
Its value ranging fraom -2,147,483,648 to 2,147,483,647 Its value ranges from 0 to +4,294,967,295
By default, type int is considered as signed integer Unsigned int is used to declare an unsigned integer
40. Define float data type.
Ans. Float data type is used to store a real number up to six digits of precision. A float uses 4 bytes of memory. Float data
type can be declared by using keyword float. The range of float is from 3.4 x 10-38 to 3.4 x 1038.
41. Write rules to name a variable in C.
Ans. Each variable must have a unique name or identifier. Following rules are used to name a variable.
 A variable name can only contain alphabets (uppercase or lowercase), digits and underscore sign.
 Variable name must begin with a letter or an underscore, it cannot begin with a digit.
 A reserved word cannot be used as a variable name.
 There is no strict rule on how long a variable name should be, but we should choose a concise length for variable
name to follow good design practice.
42. How a variable can be declared?
Ans. Declaring a variable specifies the type of variable, the range of values allowed by the variable, and the kind of
operations that can be performed on it. After declaring a variable, its data type cannot be changed.
Example: float height;
43. Can we declare multiple variables of same type in single statement?
Ans. Multiple variables of same data type may also be declared in a single statement.
Example: unsigned int age, basic_ salary, gross_ salary;
44. What is meant by initializing a variable in C.
Ans. Assigning value to a variable for the first time is called variable initialization. C language allows us to initialize a
variable both at the time of declaration or after declaring it.
Syntax (at the time of declaration): data _ type variable _ name = value;
Syntax (after declaration): data _ type variable _ name;
variable _ name = value;

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

NAME _______________ CLASS 10th


CHAPTER 1: INTRODUCTION TO PROGRAMMING
ACTIVITY SOLUTIONS
Activity 1.1: Use your web browser to find out the names of three different IDEs that can be used for C programming
language.
Ans. 1. Eclipse 2. NetBeans 3. Sublime Text
Activity 1.3: From the following list, encircle the reserved words in C language:
Ans. int, pack, create, case, return, small, math, struct, program, library
Activity 1.4: Identify different parts of the following C program:
Ans. Link section or Header section:
#include < stdio.h >
#include < conio.h >
Main section:
Void main ()
Body of main () function:
{
printf(“I am a student of class 10”);
getch();
}
Activity 1.5: Tick valid comments among the following:
Ans.
 *comment goes here*
 %comment goes here%
 /*comment goes here/
 //comment goes here*/
 /comment goes here/
 /*comment goes
here*/
Activity 1.6: Identify the type of constant for each of the following values:
Ans. Integer constants: 12, -21, 41
Real constants: 1.2, 32.768, -12.3, 40.0
Character constants: ‘*’, ‘a’, ‘\’
Activity 1.7: Encircle the valid variable names among the following:
Ans. _Hello, 1var roll_num Air23Blue float
Case $car name =color Float
Activity 1.8: Write a program that declare variable of appropriate data types to store your personal data. Initialize
these variables with the following data:
 initial letter of your name
 your marks in 8th class
 initial letter of your gender
 your height
Ans. #include < stdio.h >
void main()
{
char name = ‘u’;
char gender = ‘f’;
int age = 14;
int marks = 450;
float height = 5.8;
}

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

EXERCISE SOLUTIONS
Q1 Multiple Choice Questions:
1. A software that facilitates programmers in writing computer programs is:
(a) a compiler (b) an editor (c) an IDE (d) a debugger
2. ___________ is a software that is responsible for the conservation of program files to the machine understandable and
executable code.
(a) Compiler (b) editor (c) IDE (d) Debugger
3. Every programming language has some primitive building blocks and follows some grammar rules known as its:
(a) Auto words (b) syntax (c) building blocks (d) semantic rules
4. A list of words that are predefined and must not be used by the programmer to his own variables is known as ________.
(a) Auto words (b) reserved words (c) restricted words (d) predefined words
5. Include statements are written in _________ section.
(a) Header (b) main (c) comments (d) print
6. ________ are added in the source code to further explain the techniques and algorithms used by the programmer.
(a) Messages (b) hints (c) comments (d) explanations
7. ________ are the values that do not change during the whole execution of program.
(a) Variables (b) Constants (c) strings (d) comments
8. A float uses bytes of memory.
(a) 3 (b) 4 (c) 5 (d) 6
9. For initializing variable, we use ___________ operator.
(a) . (b) = (c) @ (d) ?
10. ________ can be thought of as a container to store constants.
(a) Box (b) jar (c) variable (d) collection
Q2 True or False
1. An IDE combines text editors, libraries, compilers and debuggers in a single interface. F
2. Computers require the conversion of the code written in program file on the machine language in order to execute it. T
3. Column is a reserved word in C programming language. F
4. *Comment goes here* is a valid comment. F
5. float can store a real number up to six digits of precision. T
Q3 Define the following:
1) IDE: A software thar provides a programming environment to facilitate programmers in writing and executing computer
programs is as an Integrated Development Environment (IDE).
Example: Xcode Visual Studio Code::Blocks Dev C++
2) Compiler: A compiler is a software that is responsible for conversion of computer program written in some high-level
programming language to machine language.
3) Reserved Words: Every programming language has a list of words that are predefined. Each word has its specific
meaning already known to the compiler. These words are known as reserved words or keywords
Example: int, if and return are reserved words in c language.
4) Main section of a program: It consist of main () function. Every C program must contain a main () function and it is the
starting point of execution.
5) Char data type: A variable of type char store one character only. To declare character type variable in C, we use the
keyword char. It takes up just 1 byte of memory for storage.
Example: char c = ‘a’
Q4 Briefly answer the following questions.
1. Why do we need a programming environment?
Ans. In order to correctly perform any task, we need to have proper tools. For example, for gardening we need gardening
tools and for painting we need a collection of paints, brushes and canvas. Similarly, we need for programming we need proper
tools for writing program. It is essential to setup a programming environment before we start writing programs. It works as a
basic platform for us to write and execute programs.
2. Write the steps to create a C program file in the IDE of your lab computer.
Ans. Step 1. Open Code::Blocks
Step 2. Click on the file and then click new
Step 3. Write C program
Step 4. Save file with extension .c
Step 5. Click on the build and run button to see the program’s output

3. Describe the purpose of compiler.


Ans. Computers only understand and work in a machine language consisting of 0s and 1s. they require the conversion of a
program written in programming language to machine language, in order to execute it. This is achieved using a compiler.

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

4. List five reserved words in C programming language.


Ans.
if for long do case
5. Discuss the main parts of the structure of C program.
Ans. A program can be divided into three main parts:
 Link section or header section
 Main section
 Body of main () function
Link Section or Header Section: While writing program in C language, we make extensive use of function that are already
defined in the language. But before using the existing functions, we need to include the files where these functions have been
defined. These files are called header files.
Syntax: #include < header file name >
Example: #include < stdio.h >
Main Section: It consists of main () function. Every C program must contain a main () function and it is the starting point of
execution.
Body of main () function: The body of main () function is enclosed in curly braces {}. All the statements inside these curly
braces make the body of main function. We can also create other functions in our program and use them inside the body of
main () function.
Example: Void main ()
{
printf(“Hello World”);
}
6. Why do we use comments in programming?
Ans. Definition: Comments are the statements in a program that are ignored by the compiler and do not get executed.
Usually, comments are written in natural language in order to provide description of our code.
Example: //This is a comment
Purpose of writing comments: Comments can be thought of as documentation of the program. There are two main
purposes of writing statements:
 They facilitate other programmers to understand our code.
 They help us to our own code even after years of writing it.
We do not want these statements to be executed, because it may cause syntax error as the statements are written in natural
language.
Types of comments in C: There are two types of comments in C language.
1. Single line comment 2. Multi line comment
Single Line Comments: Single line comments start with //. Anything after // on the same line, is considered a comment.
Syntax: //This is single-line comment.
Multi Line Comments: Multi line comments start with /* and end at */. Anything between /* and */ is considered as a
comment, even on multiple lines.
Syntax: /*This is
a multi-line
comment*/
7. Differentiate between constants and variables?
Ans. Constant Variable
Constant is a number or symbol that has fixed value Variable is a symbol used for number not yet known
Constant cannot change their value Variable can change their value
Constant are declared with keyword const Variable are not declared with keyword const
Example: Example:
float const pi = 3.14; int a = 10;
pi value does not change throughout the program value of a can be changed
8. Write down the rules for naming variables?
Ans. Each variable must have a unique name or identifier. Following rules are used to name a variable.
 A variable name can only contain alphabets (uppercase or lowercase), digits and underscore sign.
 Variable name must begin with a letter or an underscore, it cannot begin with a digit.
 A reserved word cannot be used as a variable name.
 There is no strict rule on how long a variable name should be, but we should choose a concise length for variable
name to follow good design practice.
 Blank space cannot be used in variable name.
 A variable name used for one data type cannot be used for another data type.
 C is a case sensitive language. Variables having same name but different case are considered as two different
variable names.

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

9. Differentiate between char and int.


Ans. char int
A variable of type char can store one character only. Integer data type is used to store integer values (whole
numbers).
It takes up just 1 byte of memory. Integer takes up 4 bytes of memory.
Keyword: char Keyword: int
Example: char c = ‘a’; Example: int i = 2;
10. How can we declare and initialize a variable?
Ans. Variable Declaration: Declaring a variable specifies the type of variable, the range of values allowed by the variable,
and the kind of operations that can be performed on it. After declaring a variable, its data type cannot be changed.
Syntax: data_ type variable name;
Example: float height;
Declare multiple variables of same type in single statement: Multiple variables of same data type may also be
declared in a single statement.
Example: unsigned int age, basic_ salary, gross_ salary;
Variable initialization: Assigning value to a variable for the first time is called variable initialization. C language allows
us to initialize a variable both at the time of declaration or after declaring it.
Syntax (at the time of declaration): data _ type variable _ name = value;
Syntax (after declaration): data _ type variable _ name;
variable _ name = value;
Q5 Match the columns:
Sr.# A B C
1. IDE Machine executable code Clion
2. Text Editor Include statement Notepad
3. Compiler Python Machine executable code
4. Programming Language Clion Python
5. Reserved Words /* (a + b) */ Struct
6. Link Section Notepad Include statement
7. Body of main ( ) Struct {}
8. Comment {} /* (a + b) */

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

NAME _______________ CLASS 10th


CHAPTER 2: USER INTERACTION
SHORT QUESTIONS
1. Define computer?
Ans. A computer is a device that takes data as input, process that data and generates the output.
2. What are I/O function?
Ans. We need a way to provide input and show output while writing program. Each programming language has its keywords
or standard library functions for I/O operations.
Example: printf function to display the output.
scanf function to get input from user.
3. Define printf function?
Ans. printf is a built-in function in C programming language to show output on screen. Its name comes from “printf
formatted” that is used to print the formatted output on screen.
Example: printf(“Hello world”);
4. What is a difference between printf(“age”) and printf(“%d”, age)?
Ans. printf(“age”) displays the word age on screen. And printf(“%d”, age) displays the value stored inside the variable age.
5. What are format specifiers?
Ans. Format specifiers are used to specify format of data type during input and output operations. Format specifier is always
preceded by a percentage (%) sign. These are used for: Data Type Format Specifier
 Taking input using the scanf() function Int % d or % i
 Printing output using printf() function Float %f
Example: %d is integer format specifier. Char %c
6. Write a C program that displays your age and height.
Ans. Program:
#include < stdio.h >
void main( )
{
float height = 5.8;
Output: My age is 21 and my height is 5.800000
int age = 21;
printf(“My age is %d and my height is %f, age, height);
}
7. How can we specify number of digits after decimal point in float values?
Ans. If we want to specify the number of digits after decimal point, then we can write %.nf where n is the number of digits.
Example: printf(“My age is %d and my height is %.2f, age, height);
Output: My age is 21 and my height is 5.80
8. Write a program that displays the sum of two numbers.
Ans. Format specifiers are not only used for variables. Actually, they are used to display the result of any expression
involving variables, constants, or both.
Program: #include < stdio.h >
void main ()
{ Output: Sum of 23 and 45 is 68
printf(“Sum of 23 and 45 is %d”, 23 + 45);
}
9. Define the function of scanf.
Ans. scanf is a built-in function in C language that takes input from user into the variables. We specify the expected input data
type in scanf function with the help of format specifier.
Example: scanf(“%c”, &grade);
There are two main parts of scanf function.
 First part inside the double quotes in the list of format specifiers.
 Second part is the list of variables with & sign at their left.
10. Write a C program that prints your grade on screen.
Ans. Program: #include < stdio.h >
void main ()
{
char grade = ‘A’;
scanf(“%c”, &grade);
}

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

11. Write a C program that input a number and display it on screen.


Ans. Program: #include < stdio.h >
void main ()
{
int number;
printf(“enter a number between 0-10:”);
scanf(“%d”, &number);
printf(“The number you entered is: %d”, number);
}
12. Can we take multiple inputs using a single scanf ()? How?
Ans. Yes, we can take multiple inputs using a single scanf function like scanf(“%d%d%f ”, &a, &b, &c); it takes input two
integer type variables a and b, and one float type variable c. after each input, user should enter a space or press enter key.
After all the inputs user must press enter key.
Note: Without & sign the program gets executed but does not behave as expected.
13. Define the function of getch.
Ans. getch () function is used to read a character from user. The character entered by user does not get displayed on screen.
This function is generally used to hold the execution of program because the program does not continue further until the user
types a key. To use this function, we need to include the library conio.h in the header section.
14. Write C program that exits on pressing enter key.
Ans. Program: #include < stdio.h >
void main ()
{
printf(“ Enter any key if you want to exit program”);
getch();
}
15. Write a C program that reads character using getch().
Ans. Program: #include < stdio.h >
void main ()
{
char key;
printf(“Enter any key:”);
key = getch();
}
16. What is the difference between getch and scanf?
Ans. scanf getch
When we read character through scanf it requires us In case of getch, it does not wait for enter key to be
to press enter for further execution. pressed. Function reads a character and proceed to
the execution of next line.
17. What are statement terminators?
Ans. A statement terminator is identifier for compiler which identifies end of a line. In C language semi colon (;) is used as
statement terminator. If we do not end each statement with a ; it results into error. Statement
printf(“Hello World”) ; ;
18. Define escape sequence? Terminator
Ans. Escape sequence forces printf to escape from its normal behavior. Escape sequence are used in printf function inside the
“and”. It is combination of escape character (\) and a character associated with special functionality.
19. How do you write escape sequence in C language?
Ans. Escape sequence consist of two characters. The first character is always backslash (\) and the second character varies
according to the functionality that we want to achieve. Example: \a
20. What is escape character?
Ans. Back slash (\) is called escape character which is associated with each escape sequence to notify about escape. Escape
character and character text to it or not displayed on screen, but they perform specific task assigned to them.
21. Write some escape sequence in C language?
Ans. Sequence Purpose
Displays Single Quote ( ‘ )
\
\\ Displays Back Slash ( \ )
\a Generates an alter sound
\b Removes previous character
22. Define the purpose of \n.
Ans. After escape character, n specifies movement of the cursor to start of the next line. This escape sequence is used to print
the output on multiple lines.

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

23. Write a program that displays your name in first line and your location in second line.
Ans. Program: #include < stdio.h >
void main () {
Output: My name is Ali.
printf(“My name is Ali. \n”); I live in Lahore
printf(“I live in Lahore.”);
}
24. Can we print new line without \n?
Ans. in the absence of an escape sequence, even if we have multiple printf statements, their output is displayed on a single
line.
Example: #include < stdio.h >
void main () {
printf(“My name is”); Output: My name is Luqman Ahmad
printf(“Luqman Ahmad”);
}
25. Define the purpose \t.
Ans. Escape sequence \t specifies I\O function of moving to the next tab stop horizontally. A tab stop is collection of 8 spaces.
Using \t takes cursor to the next tab stop. This escape sequence is used when user presents data with more spaces.
Example: #include < stdio.h >
void main (){
printf(“Name: \tLuqman\nFname: \tM.Nawaz\nMarks: \t1012”);
}
Output: Name: Luqman
26. List some basic operators used in C. Fname: M.Nawaz
Ans. There are following operators in C language: Marks: 1012
 Assignment  Arithmetic  Logical  Relational
27. Define assignment operator?
Ans. Assignment operator is used to assign a value to a variable, or assign a value of variable to another variable.
Equal sign (=) is used as assignment operator in C. Example: int sum = 5;
28. Define arithmetic operator?
Ans. arithmetic operators are used to perform arithmetic operations on data. Operator: +, -, *, /
29. Define division operator.
Ans. division operator (/) divides a value of left operand by the value of right operand. Example: float result = 3.0 / 2.0
After this statement, the variable result contains the value 1.5
30. What is a difference between 3.0/2 and 3/2.
Ans. If both the operands are of type int, then result of division is also of type int.
 float 3 / 2; As both values are of type int so answer is also an integer which is 1.
 float 3.0 / 2; In this case the value stored in variable result is 1.5
31. Define multiplication operator?
Ans. Multiplication operator (*) is a binary operator which performs the product of two numbers.
Example: int multiply = 5 * 5;
32. Define addition operator.
Ans. Addition operator (+) calculates the sum of two operands. Example: int add = 10 + 10;
33. Define increment and decrement operator.
Ans. The increment and decrement operators are unary operators. The increment operator ++increase the value of variable by
1. Similarly, the decrement operator -- decrease the value of variable by 1.
Example: the statement a = a + 1; is used to increase the value of a variable a by 1. In C language this statement can
also be written as a++; or ++a; .Similarly, a--; or --a; is used to decrease the value of a by 1.
34. Define subtraction operator?
Ans. subtraction operator (-) subtracts right operand from the left operand. Example: int result = 20 – 15;
35. Write a program that takes monthly income and monthly expenses and calculate monthly savings.
Ans. Program: #include < stdio.h >
void main () {
int income, expenses, savings;
printf(“Enter Monthly Income: ”);
scanf(“%d”, &income); Output: Enter Monthly Income: 50000
printf(“Enter Monthly Expenses: ”); Enter Monthly Expenses: 33000
scanf(“%d”, &expenses); Monthly savings: 17000
savings = income – expenses;
printf(“Enter Monthly Income: ”);
}

PREPARED BY: LUQMAN AHMAD


ALLIED SCHOOL
GROWING TOGETHER

36. Define modulus operator.


Ans. Modulus operator (%) is also a binary operator, which performs division of left operand to the right operand and returns
the remainder value after division. Modulus operator works on integer data types.
Example: int remaining = 14 % 3; the value stored in variable remaining is 2.
37. Define relational operators.
Ans. Relational operators compare two values to determine the relationship between values. Relational operators identify
either the values are equal, not equal, greater than or less than one another. Operator: ==, !=, >, <, ≥, ≤
38. How relational operator works?
Ans. Relational operators perform operations on two operands and return the result in Boolean expression (true or false). A
true value is represented by 1, whereas a false value is represented by 0.
39. Differentiate between assignment operator and equal to operator.
Ans. Equal to operator (= =) Assignment Operator (=)
= = operator is used to check for equality of two = operator assigns the result of expression on right side to
expressions. the variable on left side.
It checks whether right and left operands are equal or It assigns right operand to the variable on left side.
not.
40. Define logical operator.
Ans. Logical operators perform operations on Boolean expressions and produce a Boolean expression as a result.
Operator: &&, ||, !
41. Can we use logical operator with relational expressions?
Ans. Yes, as relational operation is a Boolean expression, so logical operators can be performed to evaluate more than one
relational expression. Example: 5 > 6 && 7 < 9
42. Define AND operator (&&).
Ans. And operator (&&) takes two Boolean expressions as operands and produces the result true if both of its operands are
true. It returns false if any of the operands is false. Example: 5 > 6 && 7 < 9
Truth Table: Expression Result
False && False False
False && True False
True && False False
45. Define OR operator (||). True && True True
Ans. OR operator accepts Boolean expression and returns true if at least one of the operands is true.
Example: 5 > 6 || 7 < 9 Truth table: Expression Result
False || False False
False || True True
True || False True
46. Define NOT operator (!). True || True True
Ans. NOT operator negates or reverses the value of Boolean expression. It makes it true, if it is false and false if it is true.
Truth Table: Expression Result
!(True) False
!(False) True
47. What does short circuit evaluation?
Ans. Short circuit is to deduce the result of an operation without computing the whole expression.
48. Differentiate between unary and binary operator.
Ans. Unary Operator Binary Operator
Unary operators are applied over one operand only. Binary operators require two operands to perform operation.
Example: logical not(!) operator has only one Example: All the arithmetic operators, and relational
operand. Sign operator (-) is another example of operators are binary operators. The logical operators &&
unary operator. E.g. -5. and || are also binary operators.
49. What is ternary operator?
Ans. A type of operator that is applied on three operands is known as ternary operands.
50. What is the operator precedence in C? Also write the need of operator precedence. Operator Precedence
Ans. An operator with higher precedence is evaluated before the operator with lower () 1
precedence. In case of equal precedence, the operator at left side is evaluated before the ! 2
operator at the right side. *, /, % 3
Need of the operator precedence: +, - 4
If there are multiple operators in an expression, the question arises that which >, <, >=, <= 5
operator is evaluated first. To solve this issue, a precedence has been to each operator. = =, != 6
&& 7
|| 8
= 9

PREPARED BY: LUQMAN AHMAD

You might also like