Java Programming Reference Material Unit-I: Sree Venkateswara College of Engineering N.Rajupalem Nellore
Java Programming Reference Material Unit-I: Sree Venkateswara College of Engineering N.Rajupalem Nellore
Java Programming Reference Material Unit-I: Sree Venkateswara College of Engineering N.Rajupalem Nellore
JAVA PROGRAMMING
Reference Material
Unit-I
SVCN , Kodavaluru
Page 1
II-BTech
Unit - I
INTRODUCTION
Java is an object oriented programming language developed by James Gosling & his crew.
Previously it was called OAK but its renamed to java during 1995.
Many versions evolved over the years now the current version is java JDK 8.
KEY ATTRIBUTES OF OOP
In computer science, computer languages adopt particular programming methodology to write
programs.
For example, C language uses structured programming, Assembly language uses non structured
whereas java uses object oriented programming.
All java programs are written using object oriented programming concepts.
The oop concepts or key attributes of oop are encapsulation, inheritance, and polymorphism.
Encapsulation:
Encapsulation is the mechanism of hiding the internal details and allowing a simple interface
which ensures that the object can be used without having to know its internal details.
Example: A swipe machine encapsulates/hides internal circuitry from all users and provides
simple interface for access by every user.
In java, encapsulation is achieved by binding data and methods in a single entity called class
and hiding data behind methods and allowing the interface with the public methods of class.
The following program demonstrates the concept of encapsulation.
In the above program data and methods are encapsulated in a class called Customer and data
is hidden from user by declaring them to private and interface is allowed to setter and getter
methods.
Inheritance:
SVCN , Kodavaluru
Page 2
II-BTech
Inheritance is a mechanism by which we can define generalized characteristics and behavior
and also create specialized ones. The specialized ones automatically inherit all the properties
of generalized ones.
SVCN , Kodavaluru
Page 3
II-BTech
The process of getting the output for above program requires 3steps.
Enter the source code in either editor like notepad or IDE like netbeans or eclipse and save the
program name as Demo.java
Compile the program using javac command followed by Demo.java in command prompt
which generates .class file.
Execute the program using java command followed by Demo which will generates the output.
Description about each part of a program:
The first line import the classes required for the program. The classes required will be in the
subdirectory lang of main directory java. Since the above program requires print() method of
system class the package has to be imported into the program.
The statements written between /* */ are called multiline comments which will be ignored by
compiler. It explains the operation of program to anyone reading the program.
SVCN , Kodavaluru
Page 4
II-BTech
Since java is oop language, every java program is written within a class. So we use class
keyword followed by class name Demo.
A class code starts with a { and ends with }. In between { } we can write variables and
methods.
main() is the method which is the starting point for JVM to start execution.
Since JVM calls the main() method there wont be nothing for the main() to return to JVM.
So it is declared as void type.
Since main() method should be available to JVM which is an outside program it should be
declared with an access specifier public. Otherwise it is not accessible to JVM.
The keyword static allows main() to be executed without creating an object for class Demo.
This is necessary because main() is executed by JVM before any objects are made.
To print something to the console output we use System.out.print() method. System is a class
and out is a variable in it. Print() method belongs to PrintStream class.
When we call out variable PrintStream class object will be created internally. So we write
System.out.print().
To print a string to the output we use and passed as a parameter to print() method.
Elements of a Java Program
Whitespaces: In Java, whitespace is a space, tab, or newline.We use one whitespace
character between each token in java.
IDENTIFIERS: Identifiers are the names given to a variable, class and method. This helps
to refer to that item from any place in the program. We cant start an identifier with a digit.
Ex: int x; here x is an identifier for a variable.
Literals: A constant value in Java is created by using a literal representation of it. For
example, here are some literals:
100
98.6
X
This is a test
Comments: There are three types of comments defined by Java. Single-line, multiline,
documentation comment. Documentation comment is used to produce an HTML file that
documents the program. The documentation comment begins with a /** and ends with a */.
Separators: In Java, there are a few characters that are used as separators. The separators are
shown in the following table:
Symbo
l
()
Name
Description
Parenthesis
{}
Braces
[ ]
;
,
Brackets
Semicolon
Comma
Period
SVCN , Kodavaluru
Page 5
II-BTech
KEYWORDS: There are 50 reserved keywords currently defined in the Java language. These
keywords, combined with the syntax of the operators and separators, form the definition of the
Java language. These keywords cannot be used as names for a variable, class, or method.
In addition to the keywords, java reserves true, false and null. Const and goto keywords are
reserved and meant for future use.
OPERATORS:
An operator is a symbol which tells the compiler to perform a particular operation on
operands. Ex: a + b. a and b are operands + is an operator meant for addition operation.
Categories of operators:
Arithmetic operators
Assignment operator
Unary operators
Relational operators
Logical operators or short circuit operators
Boolean operators
Bitwise operators
Ternary or conditional operator
Member operator
Instanceof operator
New operator
Cast operator.
Arithmetic operators:
Operator
Operation
example
+
Addition,
string 12+10;
String s2=one,s3=two;
concatenation
String s1=s2+s3;
Subtraction
Int x=4,y=3,z; z=y-x;
*
multiplication
Int x=4,y=3,z; z=y*x;
/
Division
Int x=4,y=3,z; z=y/x;
%
Modulus operator
Int x=4,y=3,z; z=y%x;
(gives remainder)
Assignment operator:
This is used to store a value or variable or an expression into a variable.
SVCN , Kodavaluru
Page 6
II-BTech
Ex: int x=3, y=5, z;
z= x; z=x/3+y;
Unary operators:
Operator
Operation
example
Unary minus(negates Int x=5;
System.out.print(-x));
given value).
++
Increments a variable Int x=5;
System.out.print(x++);
value by 1.
-decrements
a Int x=5;
System.out.print(x--);
variable value by 1.
Preincrement operator allows incrementation first and then other operations are performed.
Ex: int x=10;
System.out.print(++x);// gives result 11 because incrementation first happens & then
print operation happens.
Postincrement operator allows other operations to be performed first and then incrementation
happens.
Ex: int x=10;
System.out.print(x++);// gives result 10 because print operation happens
first incrementation then happens.
Predecrement operator allows decrementation first and then other operations are
performed.
Ex: int x=10;
System.out.print(--x);// gives result 9 because decrementation first happens & then
print operation happens.
Postdecrement operator allows other operations to be performed first and then decrementation
happens.
Ex: int x=10;
System.out.print(x--);// gives result 10 because print operation happens
first decrementation then happens.
Relational Operators:
Operator
Operation
example
>(greater than)
Performs element
comparison
>=(greater
than
or
equal to)
<(lesser than)
<=(lesser
than
or
equal to)
==(equal to)
!= (not equal to)
Page 7
II-BTech
Decimal Numbers:
A Decimal integer constant can consists of any combinations of digits taken
from the set 0-9.
If the constant contains 2 (or) more digits, the first digit must be something
other than zero.
Eg: 0,1,723,32156.
Octal Constants:
An Octal integer constant can consist of any combination of digits taken from
the set 0-7.
The first digit must be zero in order to identify the constant as an octal number.
Eg: 0,01,0723.
Hexa Decimal Numbers:
A Hexa Decimal integer constant must begin with either 0x, (or) 0x.
It can be consists of the digits taken from the set 0-9, and a-f {either upper case
(or) lower case}.
The letters a-f, represent the quantities 10-15 respectively.
Eg: 0x, 0x, 0x7.
Unsigned & Long Integer Constants:
An Unsigned integer constant can be identified by appending the letter u.
{either upper or lower case}
Long interval constants can be identified by appending the letter L {either
upper or lower case} to the end of the constant.
An unsigned long interval constant can be specified by appending the letters UL
{either upper case (or) lower case} to the end of the constants.
Note that u must precede the L.
Eg: 234U Unsigned Decimal Constant
0427UL Unsigned Long Octal Constant
0X72FL Long Hexa Decimal Constant
Floating Point Constants:
A floating point constant is a decimal number that contains a decimal point.
Rules:
Commas and Blank spaces cant be included.
It can contain of either decimal point (or) Exponent.
The constant can be preceded by minus sign it desired.
Eg: 0, 1., 12.3.
Scientific Notation:
When we want to represent very large values (or) very small values, instead of
representing the value of sequence of digits we can represent a floating point
constant value in a scientific notation as follows.
Mantissa E (or) e exponent.
Eg: 1.2E + 3 => 1.2 * (10^3)
Note that if no sign is represented after E it will be treated as positive.
Floating point constants are identified by appending the letter F {either upper
case (or) lower case} to the end of the constants.
Eg: 3E5F = 300000F.
Character Constants:
SVCN , Kodavaluru
Page 8
II-BTech
A character constant is a single character enclosed in single quotation marks.
The Max length of character constant is one. The single character can be
either alphabet (or) Digit (or) a special symbol.
Eg: a, 1, ?.
Note: Most computers make use of the ASCII {American Standard Code for
Information Interchange}, character set, in which each individual character is
numerically encoded.
ASCII code is a 7 bit code. ASCII values ranges from 0-127.
String Constants:
A String Constant consists of any number of consecutive characters enclosed in
double quotation marks.
Eg: ved, college, .
Data Types
It specifies the type of the data, the size of data and about the range of data.
C supports a several different types of data types.
Classifications of Data Types:
Data Types
User defined
Structures
Unions
Enumeration
Built-In
Integer
int
Built-In:
Data Type
int
unsigned int
signed
short
unsigned short
signed short
long
void
char
SVCN , Kodavaluru
Floating Point
float
double
unsigned long
signed long
char
unsigned char
signed char
float
double
+308
long double
Derived
Arrays
Functions
Pointers
4
4
1
1
1
4
8
10
Range
-32768 to 32767
0 to 65535
-32768 to 32767
-32768 to 32767
0 to 65535
-32768 to 32767
-2147483648 to
2147483647
0 to 4294967295
-2147483648 to
2147483647
-128 to 127
0 to 255
-128 to 127
3.4E -38 to 3.4E +38
1.7E -308 to 1.7E
34E -4932 to 34E +4932
Page 9
II-BTech
Note:
Float is a single precision value. {After decimal 7 digits has been considered}
Double is a double precision value. {After decimal 14 digits has been
considered}
In-Built data Types are supported by C compiler by default.
Void:
It is a special data type used for
o To specify that a function doesnt returns any value.
o To specify a function takes no arguments.
o To create generic pointers.
Eg:
1. void print (void)
2. void *ptr
Derived Data Types:
Derived Data Types has been derived from built in data types.
Various data types are discussed below.
Arrays:
An array is a collection of homogenous {similar} type of data
An array element is stored in continuous fashion.
Eg: 1) int a [10];
2) float avg [20];
Functions:
A Function is a sub program which performs a specific task, function is a topic
related to the concept of modular programming where a larger task is divided in
two smaller tasks. Each smaller is referred to as module (or) a function.
Eg: void swap ( int a , int b)
{
int t;
t = a;
a = b;
b = t;
printf (%d %d , a, b);
}
Pointers:
Page 10
II-BTech
float avg;
};
Here struct is a keyword and student is the structure name.
Unions:
As per the definition, both structure and the union are same. The difference
between structure and union is in memory allocation.
Eg: union date
{
int day;
int month;
int year;
};
Enumerations:
These are used to assign constant values to the strings.
Eg: enum color{ red, green, black,}
Here enum is the keyword and red has been assigned value zero, green to 1
and black to 2.
Variable:
A variable is an identifier that is used to represent a single data item.
The data may be numerical quantity (or) a character constant.
The data item must be assigned a value to the variable at some point in the
program. Then data item can be accessed by referring to the variable name
In other words, a variable is an identifier which changes its value during the
execution of the program.
Rules for Constructing a Variable:
A variable name is a combination of alphabets, digits and underscore.
Other than the underscore no special characters is allowed.
The first character in the variable name must be an alphabet.
The variable name should not be of keyword.
Declaration of a Variable:
Any variable which is used in the program should be declared first.
A
declaration of a variable specifies two things:
o It tells the compiler about the name of the variable.
o It specifies about the type of data that a variable can hold.
Syntax: data type variable 1, variable 2 , . . . . . . . . variable ;
Eg: int a, b, c;
The above declaration results in declaring the variables a, b, c as integer data
type.
Initialization of Variable:
Assigning a value to a variable at time of declaration is called initialization of a
variable.
Ex:- int a=20;
SVCN , Kodavaluru
Page 11
II-BTech
Operators
An operator is a symbol that informs to the computer to perform a particular
task.
The data items that operators act upon are called operands.
If the operator requires only one operant then it is called unary operator. If it
requires two then it is called Binary Operator.
C language supports a different set of operators which are listed below.
o Arithmetic Operators
o Logical Operators
o Relational Operators
o Assignment Operators
o Increment / Decrement Operators
o Bit wise Operators
o Unary Operators
o Conditional Operators
o Special Operators
Arithmetic Operators:
Arithmetic operators are basic and common operations performed using and
computer language.
There are five arithmetic Operators available in C. They are +,-,*,/,%.
In any of the arithmetic operations, if one of the operant is of higher data type
then other operant of smaller data type will also converted to higher data type
and the result is also higher data type.
For example, in the division operation if one of the operant is floating point type
and other is of integer type then the result is floating point type.
Integer divided by an integer will always give an integer.
Logical Operator:
When we want to take a decision basing on 2 (or) more conditions then we will
use logical operators.
In three logical operators available in C are
Operator
&&
||
!
Logical and: (&&)
Logical And results
false.
Purpose
and
or
not
in true if both the conditions are true otherwise the result is
Truth Table:
A
B
A&B
T
T
T
T
F
F
F
T
F
F
F
F
Logical Or: (||)
Logical Or results in true if either of the conditions is true.
SVCN , Kodavaluru
Page 12
II-BTech
The result is false if all the conditions are false.
Truth Table:
A
B
A||B
T
T
T
T
F
T
F
T
T
F
F
F
Logical not: (!)
If the condition is true then the result is false and if the condition is false the
result is true.
Truth Table:
A
!A
T
F
F
T
Relational Operators:
Relational Operators are used to perform comparison between two values.
These operators returns true (1) if the comparison condition is true otherwise
false (0).
The operators used for comparison in C are listed below.
Operator
Purpose
<
Less than
<=
less than or equal to
>
greater than
>=
greater than or equal to
==
equal to
!=
Not equal to
Assignment Operators:
This operator is used to assign a constant value (or) the result of an expression
to a variable.
In this operation Right hand side expression is evaluated first and then the
result is assigned to left hand side variable.
Operator
Meaning
=
Assign Right Hand value to LHS value
+=
Value of LHS add to value of RHS and assign it back to the
variable in
LHS
Eg: a+=2 => a = a+2
-=
Value of RHS variable will be subtracted from the value of LHS
and
assign it back to the variable in LHS.
Eg: a-=2 => a = a-2
*=
Value of LHS variable will be multiplied to the value of RHS and
Assign it back to the variable in LHS.
Eg: a*=2 => a=a*2
/=
Value of LHS variable will be divided by the value of RHS and
Assign it back to the variable in LHS.
Eg: a/=2 => a=a/2
SVCN , Kodavaluru
Page 13
II-BTech
%=
division
>>=
<<=
&=
|=
~=
Page 14
II-BTech
<<
Left Shift
>>
Right Shift
Bitwise AND: (&)
To generate a 1 bit in the result, Bitwise AND need a 1 in both the numbers.
Bitwise AND operator is called MASK operator.
Eg: a=28, b=17
a
=11100
b
=10001
a&b =10000
a&b =16
Bitwise OR: (|)
The Bitwise OR result is 1 if at least one of the numbers is 1.
Eg: a=28,b=17
a= 11100
b= 10001
a|b =11101
=> a|b = 29
Bitwise Exclusive OR: (^)
It is similar to task Bitwise OR and the result 1 is produced if 1 is present in
either but not both.
Eg: a=28, b=17
a= 11100
b= 10001
a^b=01101 => a^b =13
Bitwise 1s complement: (~)
The complement operator switches all the Bits in a binary pattern i.e., all 0s
becomes 1s and all 1s becomes 0s.
Eg: i) Bitwise complement of 12 is -13
ii) Bitwise complement of 0 is -1.
Shift Operations:
The shift operations take binary patterns and shift the bits to the left (or) right.
Keeping the same number of bits by dropping the shifted bits at the end and
filling in with 0s at other end.
C-language provides 2 types of shift operators namely left and right shift.
Left Shift: {<<}
This operator is used for left shifting .
Eg: a=00011100
a<<1 = 00111000
a<<2 = 01110000 => a<<2 =112
Right Shift: {>>}
This operator used for right shifting.
Eg: a = 00011100
a>>1 = 00001110
a>>2 = 00000111 => a>>2 = 7
Unary Operator:
These operators require only one operant.
A Unary operator usually precedes their single operand.
Sometimes the unary operators may also follow the operands.
The following are the some of the unary operators available in c.
SVCN , Kodavaluru
Page 15
II-BTech
Operator
Purpose
&
Address of a variable
!
Negation
++
Increment Operator
sizeof()
Size of the subsequent data type in Bytes
Type
forced type of conversion
Address Operator: (&)
The Address operator is used to get the address of another variable.
Eg: p= &a
Pointer Operator: (*)
Pointer Operator is used to get the content of the address which is pointer is
pointing to
Eg: int a=5, *p;
p=&a;
*p => *(1020) => 5
Unary Minus: (-)
It is used to switch the value i.e., if the condition is true the result is false and
vice-versa.
Bitwise Complement: (~)
The Bitwise complement operator switches all the bits in a given bit pattern.
Increment / Decrement Operator: (++ / --)
The type conversion is used to convert the set of declared data type to some
other required type.
Sizeof ();
The sizeof() operator is used to find the size of the data (or) variable in Bytes.
Conditional Operator: (? :)
Page 16
II-BTech
Another use is as an operator in a for looping construct.
Parenthesis for Grouping Expression:
The precedence and associativity of each operator determines the evaluation of
an expression when more than one operator is involved.
When parenthesis is put around an element of an expression then that element
is evaluated before any operation outside the parenthesis.
Eg: (2+3) * 5 = 5*5 =25
Membership Operator:
Operation
Function call
[]
->
Structure Operator
.
+
Structure Operator
Unary plus
Unary minus
SVCN , Kodavaluru
++
Increment
--
Decrement
Not operator
Ones complement
Pointer operator
&
Address operator
Sizeof
Size of an object
Type
*
/
%
+
Type cast
Multiplication
Division
Modular division
Addition
<<
Subtraction
Left shift
>>
<
<=
>
>=
Right shift
Less than
Less than or equal to
Greater than
Greater than or equal to
Page 17
Associativity
Preceden
Left to right
1st
Right to left
2nd
Left to right
3rd
Left to right
4th
Left to right
5th
Left to Right
6th
II-BTech
==
Equality
!=
&
Inequality
Bitwise AND
Bitwise XOR
|
&&
||
?:
=, *=, -=, &=, +=
Bitwise OR
Logical AND
Logical OR
Conditional Operator
Assignment Operators
Comma Operator
Left to right
Left to right
8th
Left to right
9th
Left to right
Left to right
Left to right
Right to Left
Right to Left
10th
11th
12th
13th
Left to right
15th
postfix
ternary
prefix
unary
Primary Expressions
In C, the operand in the primary expression can be a Name, a Constant, or an
Parenthesized Expression. Name is any identifier for a variable. A constant is the
one whose value cannot be changed during program execution. Any value enclosed
within parenthesis must be reduced to single value. A complex Expression can be
converted into primary expression by enclosing it with parenthesis. The following is
an example
(3*5+8) ; (c=a=5*c);
Postfix Expressions
The postfix expression consists of one operand and one operator.
Example: A Function Call, The function name is operand and parenthesis is the
operator.
The other examples are post increment and post decrement. In post
increment the variable is increased by 1, a++ results in the variable increment by
1. Similarly in post decrement, the variable is decreased by 1, a--results in the
variable decreased by 1.
Prefix Expressions
Prefix Expressions consists of one operand and one operator, the operand
comes after the operator. Examples of prefix expressions are prefix increment and
prefix decrement i.e., ++a,--a. The only difference between postfix and prefix
operators is, in the prefix operator, the effect take place before the expression that
contains operators is evaluated. It is other way in case of postfix expressions
SVCN , Kodavaluru
Page 18
7th
14th
II-BTech
Unary Expressions
A unary expression is like a prefix expression consists of one operand and one
operator and the operand comes after the operator.
Example: ++a; -b; -c; +d;
Binary Expressions
Binary Expressions are the combinations of two operands and an operator.
Any two variables added, subtracted, multiplied or divided is a binary expression
Example: a+b; c*d;
Ternary Expressions
Ternary Expressions is an expression which consists of a ternary operator pair
?:
Example: exp1:exp2:exp3;
In the example, if exp1 is true exp2 is executed else exp3 is executed.
Type Conversion:
In some situations, some variables are declared as integers and while
performing an operation on these variables we require the result as floating
point type. In such situations we use type conversion.
The type conversion is used to convert the set of declared data types to some
other required types.
Conversion can be carried out in 2 ways
o Converting by using assignment (Implicit casting).
o Using cast operator (Explicit casting).
Converting by Assignment:
The usual way of converting a value from one data type to another is by using
the assignment operator.
int a;
float b;
a=b;
In this method, if a larger data type value is assigned to smaller data type then
the value will be truncated {cut off}. This method is called Narrowing.
If a smaller data type is assigned to larger data type, this is no loss of data. This
method is called Widening.
Cast Operator:
The operator used to force this conversion is known as Cast Operator and
the process is known as type casting.
Syntax: x= (cast) expression
(or)
X= cast (expression)
Eg: int a, b;
float f;
f= (float) a/b;
(or)
f= float (a)/b;
Comments;
Comments are non executable statements.
SVCN , Kodavaluru
Page 19
II-BTech
They are used to increase the understand ability of a program.
A comment may start anywhere in the line and whatever follows fill the end of
the line is ignored.
The double slash comment is considered as a single line comment.
Eg: // This is my first c program.
Note that there is no closing symbol.
If we want to make more than one line as comment then the comment lines
should be placed within the symbols / *. . . . . . . */
Hence /*. . . . . . . .*/ these are considered as multi line comments.
However remember we cant insert a double slash style comment within the
text of a program line.
Eg: for (i=1, // program counter;) not valid
Back Slash Characters:
The Back Slash characters are used to denote non-graphic characters and other
special characters for a specific operation.
These are also known as escape sequences.
Some of the Back slash characters are listed.
Back Slash Character
\a
\n
\t
\b
\r
\f
\v
\l
\
\0
\?
\000
\xhhh
Meaning
Alert a bell character
New line {line feed}
Horizontal Tab
Back Space
Carriage return
Form feed
Vertical tab
Backslash
Single quote
Null character
Question Mark
Octal Value
Hexa decimal values
Library Functions:
C-language provides a number of library functions that carry out various
commonly used operations.
These functions have a predefined task.
Some of the commonly used library functions are listed below.
Function
abs(i)
ceil (d)
cos (d)
cosh (d)
exp (d)
floor (d)
value
SVCN , Kodavaluru
Return Type
Purpose
int
return the absolute value of i.
double
Roundup to the next integer value
double
Returns the cosine of d
double
Return the hyperbolic cosine of d.
double
Raise e to the power of d (e^d).
double
Round down to the next integer
Page 20
II-BTech
log (d)
pow (d1,d2)
d2{d1^d2}.
getchar()
putchar()
sqrt(d)
toascii(c)
tolower(c)
toupper (c)
double
double
int
int
double
int
int
int
Header Files:
C provides a number of library functions (or) inbuilt functions. All the
library functions that are available are divided in to various categories depending
on their functionality. Each category of library functions and their definitions are
placed in a separate file called Header file.
Page 21
II-BTech
o gets().
getchar ():
Single characters can be entered in to the computer by using getchar()
function.
This function doesnt require any arguments.
The general format of getchar() function is as follows.
Syntax: char-variable-name = getchar();
Eg: ch = getchar();
Here ch is a variable name and is a variable of character data type.
scanf ();
Input data can be entered in to the computer from a standard input device by
using scan f function.
This function can be used to enter any combination of numerical values, single
characters and strings.
The general format function of scan f is as follows.
scanf(control string, arg1, arg2, . . . . . . . . . . . . argn);
Where control string {also called format string or format specifiers} refers to
the format of data being received.
The control string consists of individual group of characters with one character
for each input data item. Each character for each input data item. Each
character group begin with percent sign {%}.
arg1, arg2, . . . . . . . argn specifies the list of arguments (or) variables each
preceded with an address operator (&).
The number of format specifiers must be equivalent to number of arguments.
Here, the order of the arguments is important.
Some of the format specifiers for various data types are listed below.
Type of value
Format Specifiers
char
int
float
float {Scientific Notation}
short
octal value
string
long
unsigned int
double
hexa decimal value
c
d
f
Eg:
e
h
o
s
ld
u
lf
x
int a;
float b;
char ch;
scanf (%f %d %c, &b, &a,&ch);
gets ():
gets() is an input function used to accept a string.
SVCN , Kodavaluru
Page 22
II-BTech
It contains single argument. The argument must be a data item that represents
a string.
Eg: char line [100];
gets (line);
Output functions:
The commonly used output functions are
o putchar()
o printf()
o puts()
putchar ():
Single characters can be displayed on the standard output device {monitor} by
using the c library function put char.
This function is complementary to the input function getchar(). It transmits a
single character to the output device.
The character being transmitted will be of character type variable.
Syntax: putchar (char variable);
Eg: char c;
putchar (c);
printf();
Output data can be written from the computer on to a standard output device
using the library function printf ().
This function can be used to output any combination of numerical values, single
characters and strings.
The general format for print f statement is as follows.
printf(control string arg1, arg2 . . . . . . . argn).
Where control string refers to a string that contains formatting information and
arg1, arg2 . . . . . . . . argn are arguments that represent the individual output
data items.
It contrast, to scanf(), the arguments in a printf function do not represent
memory address and therefore they are not preceded by &.
Eg: int a;
float f;
char h;
printf (%c %f d, ch, f, a);
The printf () statement can also be used to display a message
Syntax: printf (message);
Eg: print f ( welcome to c programming);
puts ();
puts () function is used to print a strings on standard output device.
It accepts a single argument.
The argument must be a data item that represents a string.
Eg:
char
line[80];
puts (line);
Statements:
A statement in a computer program called out some action.
There are three types of statements used in c language.
o Expression statement
SVCN , Kodavaluru
Page 23
II-BTech
o Compound Statement
o Control Statement
Expression Statement:
An Expression statement consists of a valid c expression followed by
semicolon.
The expression statement used to evaluate a group of expressions.
Eg: sum = x + y;
avg = (n1+n2+n3)/3;
a = 10;
Compound Statement:
A group of valid c expressions placed within { and } is called compound
statement.
The individual statement may be an expression statement, a control statement
(or) even a compound statement.
The compound statement is not ended with a semicolon.
Eg: {
r = n%10;
s = s + (r*r*r);
n = n\10;
}
Control Statement:
The control statement is used for program flow and to check the condition of
given expression.
The keywords of the control statements are predefined and the programmer
may not use them.
Eg: if (a>b)
printf (a is big);
else.
printf (b is big)
Symbolic Constants:
There are two ways for creating symbolic constants.
o Using the quantifier constant
o Defining a set of integer constant using enum keyword.
Using the qualifier Const:
Any value declared as const cant be modified during the execution of the
program in any way.
In C we can use const in a constant expression as follows.
Syntax: const data-type variable name = value;
Eg: const float PI = 3.14;
Similarly we can create constants by using the pre-processors statement
#define.
Syntax: #define variable value
Eg: #define PI 3.14
The above statement should not end with a semi colon.
SVCN , Kodavaluru
Page 24
II-BTech
const is used to create typed constants where as #define allows to create
constants that have no type information.
Note:
As with long and short if we use const modifier they will be treated as int.
In general constants are represented in capital letters or upper case letters.
Using enum keyword:
By using enumerated data type we can assign values to a list of strings.
Eg: enum color{Black, Blue, Green}
Here Black = 0, Blue = 1, Green = 2
Structure of C Program:
Every programming language has a certain structure which has to be followed
while writing programs in that language.
C language also contains a structure to write programs.
As this language supports modular programming, it contains one or more
functions.
One of those functions that should be mentioned in every c-program is main
function.
C language contains one or more sections in a program. The structure of C
program is given below.
o Documentation Section
o Link Section
o Global Declaration
o main ().
Declarations
Executable Statements
o Sub functions
Documentation Section:
In general documentation section consists of information about the program. It
contains details like author of the program, task of the program, date compiled,
date written ,etc .
All the above information is placed as comments.
This section is optional.
Link Section:
The link section provides the instructions to the compiler about library functions
by linking to various header files.
In general the link section contains pre-processor statements.
Eg:
#include <stdio.h>
#include <conio.h>
#define PI 3.14
This section is optional
Global Declaration:
When a variable is declared inside a function the variable scope is within that
function only. These variables are called local variables.
If we want to make a variable available throughout the program irrespective of
the function then the variable is declared as global.
SVCN , Kodavaluru
Page 25
II-BTech
Any variable declared above the main function is considered as global
variables.
main ():
Every C program execution starts at main().
Every program must include main().
The entire c program instructions are divided in to two parts namely
o Declarations
o Executable statements
Declarations:
Any variable which is used in the program should be declared first. C program
doesnt allow to declare the variables at the middle of the program.
Executable Statements:
It contains various executable statements that perform a specific task.
The above two parts must be placed in a pair of braces ({ and }).
Note that each and every statement in a c program must end with semicolon
except the pre-processor statements.
Sub-Functions:
This section contains user defined functions.
In general, these functions are placed immediately after the main function.
But in practice we have a number of situations where we can change the order
of execution based on certain conditions.
The above situation involves a kind of decision making to check whether a
particular statement should be executed are not depending on the result of the
condition.
When a program breaks the sequential flow and jumps in to another part of the
code, then it is called branching.
When the branching is based on a condition, it is known as conditional branching.
It branching takes place without any decision, it is known as unconditional
branching.
C language supports the following statements, which are known as control (or)
conditional (or) Decision making statements.
If Statement
Switch Statement
Conditional Operator
If Statement:
The If statement is a powerful decision making statement.
It is used to control the flow of execution of statements.
SVCN , Kodavaluru
Page 26
II-BTech
It is basically a two-way decision statement
Entry
True
False
Two-way branching
It evaluates the test expression first and checks whether the condition is true (or)
false. It has two paths, one for true condition and other for false condition.
The If statement may be implemented in different forms.
Simple If statement:
The general form of if statement is as follows.
If (test expression)
{
Statement block;
}
Statement -x;
The statement block may be a single statement (or) group of statements.
If the test expression is true, the statement block will be executed otherwise the
block will be skipped and executes statement X.
Entry
Test
True
Expression
?
Statement
Block
False
Statement-x
Note:
The default scope of if statement is the immediate next statement i.e., if braces
are not mentioned only the immediate next statement of if will have the effect.
If-else statement:
The general form for if-else statement is as follows.
if (test expression)
{
true-block statement;
SVCN , Kodavaluru
Page 27
II-BTech
}
else
{
false-block statement;
}
Statement-x;
If the text expression is true, the true block statements will be executed
otherwise false block statement will be executed.
In either case only one block will be executed but not both.
Entry
False
Test
Expression
?
False block
Statement
True
True block
Statement
statement-x
Page 28
II-BTech
Entry
Test
Cond-1
?
True
Test
Cond- 2
True
False
False
Statement-3
Statement-2
Statement -3
statement x
Else-if ladder:
This is the another way of multipath decision making in contains a chain of ifs in
which the statement associated with each else is an it. The general form is as
follows.
If (condition -1)
Statement-1;
else if (condition -2)
Statement-2;
else if (condition-3)
Statement -3;
else
default statement;
Statement x;
The above construct is known as else if ladder. The conditions are evaluated from
top, to bottom. If condition is true, the statement associated with it is executed
and the control transfer to statement-x. When all conditions becomes false,
default statement will be executed.
True
SVCN , Kodavaluru
Cond-1
?
False
Page 29
II-BTech
Statement-1
True
Cond-2
False
Statement-2
True
cond-3
False
Default
Statement-3
Statement
Statement-x
Switch Statement:
The draw backs of nested if else are:
o Care needs to be exercised to place else to the corresponding if and
o Care needs to be exercised to place closing brace to the corresponding
opening brace as the number of conditions increases.
From the above, we can conclude that as the number of conditions increases, the
complexity of the program also increases. If statement cant handle this type of
situation perfectly.
To overcome the above problem, C provides another built-in multi way decision
making statement known as switch.
The switch statement tests the value of the given variable (or) expression against
a list of case values. When a match is found, a block of statements associated
with that particular case is executed.
The general form of Switch statement is as follows.
switch (expression)
{
case value -1: block -1;
break;
case value -2: block -2;
break;
:
:
:
:
SVCN , Kodavaluru
Page 30
II-BTech
case value n: block-n;
break;
default: default- block;
}
Statement-x;
In above syntax the expression is an integer expression (or) character.
value-1, value-2, . . . . . . . value-n are constants {int (or) char} known as case
labels, each of these values should be unique.
Block-1, block-2, . . . . .block-n are statement lists and may contain zero (or) more
statements.
There is no need to have to put braces around the block of statements but colon
is mandatory after the case label.
In case of switch, the value of expression is compared against the values value-1,
value-2,value-n.
If a case is found the corresponding block of statements will be executed.
When a match is found, the switch statement executes the matched case as well
as all the cases below it. In order to overcome this problem break statement is
used.
The break statement causes an exit from the switch statement and transfers the
control to the next statement following the switch.
The default is an optional case. If present, it should be the last case of the switch.
The default block statements will be executed when the expression doesnt
match with any case value.
The selection process of switch statement is as follows.
Switch
Exp
Expression=Value-1
block-1
Expression = Value-2
block-2
Expression = value-n
block-n
SVCN , Kodavaluru
Page 31
II-BTech
Entry
Entry
False
Body of
Test
Cond
Loop
True
False
Test
Cond
Body of
Loop
SVCN , Kodavaluru
True
Page 32
II-BTech
Entry
Exit controlled
Controlled loop.
Loop
do while Statement:
It is an exit controlled looping statement the basic format of do. While
statement is as follows.
Initialization;
do
{
body of the loop;
}while(condition);
The process of execution of do-while statement is similar to that of while
statement the difference between while & do-while is the place where the test
condition occurs.
In case of do-while statement, the program proceeds to evaluate the body of the
loop first. At the end of the loop, the test condition is evaluated.
SVCN , Kodavaluru
Page 33
II-BTech
If the condition is true, the body of the loop is once again executed. The above
process repeats until the test condition fails and then control goes out of the dowhile constructs.
for Statement:
while and do-while are indefinite looping statements i.e., these statements are
used when the numbers of iterations are not known in advance where as for is a
definite loop i.e., for statement is used when the number of iterations are known
is advance.
It is another entry controlled loop that provides a more concise looping structure.
for (initialization; test condition; increment / decrement)
{
body of the loop.
}
The execution of for statement is as follows.
Initialization of counter variable is done first.
The value of counter variable is tested using test condition. If the condition is
true the body of the loop is executed otherwise the loop is terminated.
When the body of the loop is executed, the control is transferred back to the for
statement after evaluating the last statement.
Now, the counter variable is increment/ decremented and then the new value is
again tested using test condition. If the condition is true the above process
continues otherwise the loop control goes out of the structure.
Break Statement
The break statement is frequently used to terminate the processing of a
particular case within a switch statement. Lack of an enclosing iterative or
switch statement generates an error.
Within nested statements, the break statement terminates only the do, for,
switch, or while statement that immediately encloses it.
We can use a return or goto statement to transfer control elsewhere out of the
nested structure.
The below example illustrates the break statement:
#include <stdio.h>
void main()
{
char c;
for(;;)
{
printf("\nPress any key, Q to quit: " );
// Convert to character value
scanf_s("%c", &c);
SVCN , Kodavaluru
Page 34
II-BTech
if (c == 'Q')
break;
}
} // Loop exits only when 'Q' is pressed
Continue Statement
Continue statement is a jump statement.
The continue statement can be used only inside for loop, while loop and dowhile loop.
Execution of these statement does not cause an exit from the loop but it
suspend the execution of the loop for that iteration and transfer control back
to the loop for the next iteration.
For Example , a continue statement in a for statement causes the first
expression of the for statement to be evaluated. Then the compiler
reevaluates the conditional expression and, depending on the result, either
terminates or iterates the statement body.
The below example illustrates the continue statement:
#include <stdio.h>
void main()
{
int i;
for(i=1;i<=5;i++)
{
if(i==3)
continue;
printf(%d,i);
}
getch();
}
Unconditional statements:
goto :
C language supports the goto statement to branch unconditionally from one
part of the program to another part.
Control may be transferred to any other statement with in the program i.e., the
label can be either before (or) after the goto statement.
The target statement must be labeled and the label must be followed by a
colon as follows.
Label : target statement ;
label-1: statement;
--------------SVCN , Kodavaluru
goto label-2;
Page 35
II-BTech
--------------goto label-1;
Backward goto
label-2: statement;
Forward goto
Objective Questions
1. What type of errors are checked during compilation
(a) logical errors (b) divide by zero error (c) run - time errors
(d) syntax
errors
2. The purpose of main function is
(a) to stop program execution
(b) to stop algorithm
(c) to start algorithm
(d) to start program execution
3. What is the valid identifier in the following
(a) 1fdasgf
(b) @hgd12
(c) fahs%*
(d) q1234
4. What is range of char data value?
(a) -64 to 64
(b) -128 to 127
(c) 0 to 255
(d) -127 to
128
5. The minimum number of temporary variable needed to swap the contents of two
variable is
(a) 3
(b) 1
(c) 0
(d) 2
6. What is output of following program ?
main( )
{
Int x;
x= 4 + 2 % 8;
printf(%d,x);
}
(a) 6
(b) 4.25
(c) 4
(d) -6
7. When && operator is used with two conditions, conditions need to be satisfied for
the expression to be true.
(a) neither
(b) only first
(c) any one
(d) both
8. Bitwise operators cannot be performed on
(a) float
(b) long int
(c) int
(d) char
9. In the expression b=6.6/a+(2*a+(3*c)/a*d)/(2/n); which operation will be
performed first?
(a) 2*a
(b) 2/n
(c) 3*c
(d)
6.6/a
10. What is the output of printf(%d,printf(tim));
(a) garbage
(b) results in a syntax error
(c) printf tim and terminates abruptly
(d) tim3
11. What is output of the following program?
main( )
{
int x=15,y;
y=(x >5)? 3 : 4;
printf(%d,y);
}
(a) 2
(b) 4
(c) 3
(d) 1
SVCN , Kodavaluru
Page 36
II-BTech
12. Identify the loop construct:
(a) if-else
(b) goto
(c) while
(d) switch-case
13. The index or subscript value for an array of size n ranges from
(a) 1 to n-1
(b) 0 to n-1
(c) 1 to n
(d) 0 to n
14. Which of the following is a correct way of defining a symbolic constant pie in C
(a) # define pie = 22/7
(b) #define pie 22/7
(c) #define pie= 3.142
(d) # Define pie 22/7
15. What symbol is used to represent the connector
(a) parellogram (b) rectangle with rounded end
(c) circle
(d)
rectangle
16. The ANSI C standard recognizes maximum length of a variable up to
(a) 8 characters (b) unlimited characters
(c) 31 characters (d) 15
characters
17. Which of the following is an incorrect variable name.
(a) else
(b) name
(c) string
(d) age
18. What is the size of long double variable
(a) 10 bytes
(b) 8 bytes
(c) 4 bytes
(d) 16
bytes
19. Variables are initialized in C, using
(a) : =
(b) >
(c) =
(d) =
=
20. If a is float variable, a=5/2 will return a value
(a) 2.5
(b) 2
(c) 2.0
(d) 3
21. int a=13,b=7,c;
c=a&b what is the value of c
(a) 0000 0000 0000 0101
(b) 0000 0000 0000 0100
(c) 0000 0000 0000 0110
(d) 0000 0000 0001 0101
22. In the following, which is bitwise operator?
(a) >
(b) *
(c) |
(d) <
23. If y is of integer type variable then the two expressions.
3*(y-8)/9 and (y-8)/9*3 yield the same value if
(a) y-8 is an integer multiple of 3
(b) y is an odd number
(c) y is an even number
(d) y-8 is an integer multiple of 9
24. What is output of the following program?
main()
{
int a;
float b;
a=1/2;
b=1.0/2.0;
printf( a=%d b=%f,a,b);
}
(a) a=0.0 b=0. 5
(b) a=0 b=0. 5
(c) a=0.0 b= 5
(d) a=0.5 b=0. 5
25. In switch (expression) statement, the expression should evaluate to
(a) an integer
(b) a void
(c) a float
(d) a character
26. The minimum number of times the for loop is executed is
(a) 0
(b) cannot be predicted (c) 1
(d) 2
SVCN , Kodavaluru
Page 37
II-BTech
27. char x[10]=whatisit;
char *y=whatisit;
Pick the correct answer
(a) The output of puts(x) and puts(y) will be different
(b) The output of puts(x) and puts(y) will be same
(c) The output of puts(y) is implementation dependant
(d) The outputs of pulse(x) and puls(y) compiler dependant
28. Symbolic constants are defined as
(a) #define s1 s2;
(b) #define s1=s2
(c) #define s1 s2
(d) #define s1=s2;
29. Representing various steps in a flow diagram is called as
(a) diagram
(b) flow chart
(c) program
(d) paint
30. C is a
(a) Machine language
(b) High level language
(c) High level language with some low level features
(d) Low level language
31. What are the smallest individual units in a program
(a) Structures
(b) Functions
(c) record
(d) Tokens
32. Which of the following is an empty data type.
(a) Integer
(b) float
(c) character
(d) void
33. The operator ++ is called as operator
(a) special increment (b) increment
(c) double addition
(d)
decrement
34. Which of the following is not a relational operator
(a) < =
(b) & &
(c) !=
(d) >
35. main ( ) { int m,y;
m = 5;
y = ++m;
printf(%d %d,m,y);
} consider the above code and find the output
(a) 5,5
(b) 5,6
(c) 6,6
(d) 6,5
36. int C;
C=25/2; What is the value of C
(a) 0.5
(b) 12.500000
(c) 12.000000
(d) 12
37. Consider scanf(%2d, &n); and the input data is 3142 then n will be assigned
(a) error
(b) 31
(c) 42
(d) 31.42
38. In switch statement
(a) more than one default allowesd (b) default case, if used, should be the
last case
(c) default case, if used, can be placed anywhere (d) default case must be
present
39. How many while statements are possible in do.... While loop?
(a) 2
(b) 3
(c) any number (d) 1
40. Which of the following is the symbol for preprocessor
(a) *
(b) #
(c) <>
(d) $
41. Pseudo code is
(a) language independent code
(b) refined version of program
(c) code developed using the syntax of a specific language
(d) outcome of compilation process
42. A block is enclosed with pair of
SVCN , Kodavaluru
Page 38
II-BTech
(a) ( )
(b) { }
(c) <>
(d) [ ]
43. Which of the following cannot be used as an identifier.
(a) alphabet followed by digit (b) alphabet
(c) Keywords
(d) Library
function name
44. Size of (double) returns
(a) 4
(b) 8
(c) 10
(d) 2
45. Which of the following statements is wrong
(a) con = T * A ;
(b) This = T * 20 ;
(c) 3+a=b;
(d) mes =
123.56
46. main( )
{ int a=5; float b=10,c;
c=b%a;
printf(%f,c);
}
output is
(a) gives an error
(b) 2.0
(c) 0.00
(d) 0.000000
47. int a=13,b=7,c;
c=a&b what is the value of c
(a) 0000 0000 0000 0101
(b) 0000 0000 0001 0101
(c) 0000 0000 0000 0100
(d) 0000 0000 0000 0110
48. int x=1,y=5;
x=++x + y;
what is the value of x
(a) 8
(b) 6
(c) 5
(d) 7
49. In the expression b=6.6/a+(2*a+(3*c)/a*d)/(2/n); which operation will be
performed first?
(a) 2*a
(b) 2/n
(c) 3*c
(d) 6.6/a
50. Consider the segment
If(1) printf(yes);
else printf(no);
what will be the output
(a) no
(b) Unpredictable (c) yes
(d) Error
51. In switch (expression) statement, the expression should evaluate to
(a) a character
(b) a void
(c) an integer
(d) a float
52. Which of the following statements is false
(a) The initialization and increment parts of a for statement can be empty
(b) The body of do-while statement can be empty
(c) The expression in the condition part of a for statement can be empty
(d) The initialization part of a for statement cannot have more than one
initialization
53. The function sqrt() is part of header file.
(a) iostream.h
(b) conio.h
(c) math.h
(d) stdio.h
54. The process of repeating a group of statements in an algorithm is known as
(a) sequence
(b) iteration
(c) flow
(d) selection
55. Every executable C program must contain a
(a) printf function(b) scanf, printf and main functions(c) main function(d)
scanf function
56. The individual units of a C program is known as
(a) records
(b) tokens
(c) units
(d) program
SVCN , Kodavaluru
Page 39
II-BTech
57. The keyword is used to define enumerated data type
(a) array
(b) enum
(c) typedef
(d) struct
58. How many variables of the same type can be initialized at a time with the same
value
(a) Three
(b) One
(c) Two
(d) any number of
variables
59. Integer division results in
(a) rounding
(b) underflow
(c) overflow
(d)
truncation
60. The operator with lowest priority in the list && , + , | | , < is
(a) +
(b) | |
(c) &&
(d) <
61. int x=1,y=5;
x=++x + y;
what is the value of x
(a) 8
(b) 7
(c) 5
(d) 6
62. Which of the following shows the correct hierarchy of arithmetic operations in C
(a) ( ), / or *, - or +
(b) ( ), &&, *,/,+,- (c) ( ), &&, / , *, +, (d)
( ),&&,*or / + or -;
63. The statement printf(%d,7);
(a) prints 0
(b) runtime error (c) prints the value 7(d) results in syntax
error
64. The keyword else can be used with
(a) for statement (b) if statement (c) switch ( ) statement (d) do.. while ( )
statement
65. Give the output of the following program:
#include < stdio.h >
main()
{
int I=1;
while (I < 5)
{
printf(%d, I);
}
} /* End of Main */
(a) Warning for no return type for main ( )(b) Print the value of I as 1
(c) Infinite loop
(d) Prints the value of I as11111
66. putchar() and getchar() are both
(a) control statement
(b) type declaration statements
(c) assignment statements
(d) input and output
statements
67. The process of removing a bug is known as
(a) debugging
(b) compilation
(c) executing
(d) running
68. Every C Program Statement must be terminated with a
(a) .
(b) #
(c) ;
(d) !
69. In the following which one is not a C keyword?
(a) choice
(b) case
(c) for
(d) volatile
70. What is the range of unsigned char data type
(a) 0 to 512
(b) 0 to 255
(c) -128 to 127
(d) -32, 768 to
32,767
SVCN , Kodavaluru
Page 40
II-BTech
71. C variable cannot start with
(a) a lower case (b) an underscore
(c) a number
(d) an upper case
letter
72. Which of the following is allowed in a C arithmetic statement
(a) ( )
(b) [ ]
(c) { }
(d) / /
73. What is the result of 5 &&2 ?
(a) 0
(b) 2
(c) 1
(d) 5
74. ++I is a operator.
(a) Pre-decrement
(b) Pre-increment
(c) Post increment
(d)
Post decrement
75. Which of the following shows the correct hierarchy of arithmetic operations in C
(a) ( ), &&, *,/,+,- (b) ( ), &&, / , *, +, (c) ( ),&&,*or / + or -; (d) ( ), / or *,
- or +
76. The output of the following program is
main( )
{
int i=2;
printf(%d %d %d,i++,i,++i);
}
(a) 2 2 4
(b) 2 3 3
(c) 3 3 3
(d) 2 3 4
77. In switch (expression) statement, the expression should evaluate to
(a) a void
(b) an integer
(c) a character
(d) a float
78. How many while statements are possible in do.... While loop?
(a) any number (b) 2
(c) 3
(d) 1
79. How would you declare a constant of 5 called MYCONST?
(a) var int MYCONST=5
(b) int myconst = 5;
(c) constant MYCONST = 5; (d) #define MYCONST 5
80. What type of errors are checked during compilation
(a) divide by zero error (b) logical errors (c) run - time errors
(d) syntax
errors
81. The constant \b is used for
(a) tab
(b) vertical blank (c) bell
(d)
backspace
82. Which of the following is a valid numeric constant
(a) 20,000
(b) 15 750
(c) $1000
(d) 65432
83. What is range of char data value?
(a) -128 to 127
(b) -64 to 64
(c) 0 to 255
(d) -127 to
128
84. Which of the following is the assignment operator in C
(a) !=
(b) = =
(c) =
(d) : =
85. What is output of following program ?
main( )
{
int x;
x= 4 + 2 % 8;
printf(%d,x);
}
(a) 4
(b) -6
(c) 6
(d) 4.25
86. The equality relational operator is represent by
SVCN , Kodavaluru
Page 41
II-BTech
(a) :=
(b) ==
(c) .EQ.
(d) =
87. main( )
{ int a=0; if(a) printf(%d,++a); else printf(%d, a+=2) ; } the output is
(a) 3
(b) 1
(c) 2
(d) 0
88. x=9-12/3+3*2-1, what is the value of x
(a) -10
(b) 10
(c) 4
(d) 2
89. The function echoes the character typed on the screen
(a) getchar()
(b) gets()
(c) getche()
(d) getchr()
90. Which of the following statement is not true about the switch statement
(a) Character constants are automatically converted into integer
(b) In switch() case statement nested if can be used
(c) The switch() can evaluate relational or logical expressions
(d) No two case statements have identical constants in the same switch
91. while(++I <=n);
what is the value of I when the loop completes, if the initial value of I is 1.
(a) n-1
(b) n+2
(c) n+1
(d) n
92. #include is a directive
(a) processor
(b) complier
(c) pre-compiler (d) preprocessor
93. What will be sum of the binary numbers 1111 and 11001
(a) 111100
(b) 100010
(c) 11110
(d) 101000
94. Which one of the following is known as the language of the computer
(a) Programming language
(b) Machine language
(c) High level language (d) Assembly level language
95. Find the output
void main()
SVCN , Kodavaluru
Page 42
II-BTech
{char a[]=12345\0;
int i=strlen(a);
printf(here in 3 %d\n,++i);}
(a) here in 3
(b) here in 3 6
(c) 6
(d) 3
96. Which of the following is syntactically correct
(a) for(;);
(b) for();
(c) for(,);
(d) for(;;);
97. Find out the output for the following
#include<stdio.h>
main() {
int c=--2;
printf(c=%d,c);}
(a) -2
(b) 0
(c) 2
(d) None
98. Identify the result
void main()
{ int i=5;
printf(%d,i+++++i);}
(a) 5
(b) 6
(c) 10
(d) compiler error
99. which one of the following is not a translator program
(a) Assembler
(b) Interpreter
(c) Linker
(d) Compiler
100. What will be the ASCII Octal value of A
(a) 100
(b) 101
(c) 110
(d) 111
101. a<<1 is equal to
(a) multiplying by 2
(b) dividing by 2 (c) added 2
(d) None
102. Consider the following and find the output
main()
{ int a=0;int b=30;char x=1;
if (a,b,x)
Printf(Hello);
}
(a) compiler error
(b) abxHello
(c) Hello
(d) None
II Fill in the blanks
103. The process of repeating a group of statements in an algorithm is known
as___________
104. Extend the term CPU ________________________________
105. Monitor, keyboard, mouse and printers are _________devices
106. C was developed by ____________________
107. _________is used to compile your c program
108. Short Integer size is ____ bytes
109. The while loop repeats a statement until the test at the top proves _______
110. The __________statement transfers control to a statement within its body
111. The _______is a unconditional branching statement used to transfer control of
the program from
one statement to another
112. ANSI stands for __________________________________________
113. What type of errors are checked during compilation
(a) logical errors (b) divide by zero error
(c) run - time errors (d)
syntax errors
114. Which one of the following numeric value is used to represent base of the
binary number
SVCN , Kodavaluru
Page 43
II-BTech
(a) 8
(b) 10
(c) 2
(d) 16
115. What will be the binary value of B
(a) 1001
(b) 1011
(c) 1100
(d) 1101
116. Which of the following is not a translator program
(a) Linker
(b) Assembler
(c) Compiler
(d)
Interpretor
117. The program fragment
int a=5, b=2;
printf(%d,a+++++(b) ;
(a) prints 7
(b) prints 8
(c) prints 9
(d) none of the above
118. Consider the following program segment. i=6720; j=4;
while((i%j)==0)
{ i=i/j; j=j+1;
}
On termination j will have the value
(a) 4
(b) 8
(c) 9
(d) 6720
119. #include<stdio.h>
main()
{
int i=1,j=2;
switch(i)
{
case 1: printf("GOOD");
break;
case j: printf("BAD");
break;
}
}
(a) GOOD
(b) BAD (c) GOOD BAD (d) Compiler Error
II Fill in the Blanks
120. ALU stands for ____________
121. ________ translates the high level language source code into low-level language
122. The size of long double variable is _________
123. The ___________of an operator gives the order in which expressions involving
operators of the same precedence are evaluated.
124. The output of the assembler in the form of sequence of 0s and 1s is called
_________
125. The process of repeating a group of statements in an algorithm is known as
_______
126. The # symbol is known as ________________
127. ______________ are identifiers reserved by the C language for special use
128. The _________of an operator gives the order in which operators are applied in
expressions
129. _____________ is very similar to the while loop except that the test occurs at the
end of the loop body
SVCN , Kodavaluru
Page 44
II-BTech
130. What are tri-graph characters? How they are useful.
C introduces the concept of tri-graph sequences to provide a way to enter
certain characters that are not available on some key boards. Each tri-graph
sequence consists of three characters- two question marks followed by another
character. Some of the tri- graph characters are listed below.
Tri-graph Sequence
??=
??(
??)
??<
??>
??!
??\
??/
??-
Translation
# number sign
[ left bracket
] right bracket
{ left brace
} right brace
| vertical bar
\ back slash
^ caret
~ tilde
131.
What will be the output of the following program
#include<stdio.h>
void main()
{
int i=2,j=3,k,l;
float a,b;
k=i/j*j;
l=j/i*I;
a=i/j*j;
b=j/i*I;
printf(%d%d%f%f,k,l,a,b);
}
Ans:-____________________
132.
What will be the output of the following program
#include<stdio.h>
void main()
{
int a,b;
a=-33;
b=-3(-3);
printf(a=%db=%d,a,b);
}
133.
What will be the output of the following program
#include<stdio.h>
void main()
{
float a=5,b=2;
int c;
c=a%b;
printf(%d,c);
SVCN , Kodavaluru
Page 45
II-BTech
}
134.
What will be the output of the following program
#include<stdio.h>
void main()
{
printf(nn \n\n nn\n);
printf(nn/n/nnn/n);
}
135.
What will be the output of the following program
#include<stdio.h>
void main()
{
int a,b;
printf(Enter values of a and b);
scanf(%d%d,&a,&b);
printf(a=%d b=%d,a,b);
}
136.C language has been developed by
a Ken Thompson (b) Dennis Ritchie
(c) Peter Norton (d)Martin
Ritchards
137.C can be used on
a MS-DOS
(b) LINUX
(c)Windows
(d)All The Above
138.C programs are converted into machine language with help of
(a) An editor
(b)A compiler
(c)An operating System (d)
None
139.The real constant in CV can be expressed in which of the following forms
(a)Fractional Form
(b)Exponential Form
(c)ASCII form
(d)
Both a and b
140. A character variable at a time can store
(a) 1
(b) 8
(c) 254
(d)none
141.The statement char ch=Z would store in ch
(a) Character Z
(b)ASCII value of Z
(c)Z along with inverted
commas (d)Both a and B
142. Which of the following is not a character constant
a Thank You
(b)Enter value of p
(c) 23.5e-03
(d) all
of the above
143.The maximum value that an integer constant can have is
a -32767
(b)32768
(c)32737
(d)32767
144.A C variable cannot start with
a An alphabet
(b)a number
(c)a special symbol other than
under score (d) None
145.Which of the following statements is wrong
(a)mes=123.56;
(b)con=T*A;
(c)this=T*20;
(d)3+a=b;
146. Which of the following shows correct hierarchy of arithmetic operators in c
(a)-,*,/,+
(b)+,-,*,/
(c)/ or *,- or +
(d) None
147.In b=6.6/a+2 *n; which operation is performed first?
(a)6.6/a
(b)a+2
(c)2*n
(d)depends on compiler
SVCN , Kodavaluru
Page 46
II-BTech
148. Which of the following is allowed in a C Arithmetic instruction
(a) { }
(b)[ ]
(c)( )
(d) None
149. Which of the following statements is false
(a) Each new C instruction has to be written on a separate line
(b) Usually all c statements are entered in small case letters
( c ) Blank spaces may be inserted between two words in a c statement
(d) Blank spaces cannot be inserted within a variable name
150. If a is an integer variable, a=5/2 ; will return a value
(a) 2.5
(b)3
(c)2
(d)0
151. The expression a=7/22*(3.14+2)*3/5; evaluates to
(a)8.28
(b)6.28
(c)3.14
(d)2
152. If a is a integer ,the expression a=30*1000+2768 evaluates to
(a)32768
(b)-32768
(c)113040
(d)0
153. The expression x=4+2%-8 evaluates to
(a)-6
(b)6
(c)4
(d)None
154. Hierarchy decides which operator
(a)is most important
(b)is used first
(c)is fastest
(d)largest
155. An integer constant in c must have
(a)at least one digit
(b)At least decimal point (c)a comma along the
digits(d)digits with comma
156. In C a variable cannot contain
(a)Blank space
(b)hyphen
(c)decimal point (d)all
157. Which of the following is false in c
(a) Keywords can be used as variable names
(b) variable names can contain a digit
(c )Varaible names do not contain a blank space
(d) capital letters can be used in variable names
158. In C, arithmetic instruction cannot contain
(a) variables
(b)constants
(c)variables on right hand side
of =
(d)constants on left hand side of =
159.Which of the following is odd one out
(a) +
(b) (c) /
(d)**
160.What will be the value of d(assume d to be a float) after the operation
d=2/7.0?
(a)0
(b)0.2857
(c)cannot be determined (d)none
161.An expression contains relational operators ,assignment operators and
arithmetic operators. In the absence of parenthesis ,they will be evaluated in
which of the following order:
a Assignment ,relational,arithmetic
b Arithmetic,relational,assignment
c Relational,arithmetic,assignment
d Assignment,arithmetic,relational
162.The break statement is used to exit from:
(a)an if statement
(b)a for loop
(c) a program
(d)the
main() function
163. A do-while loop is useful when we want that the statements within the
loop must be executed:
SVCN , Kodavaluru
Page 47
II-BTech
(a) only once
(b)At least once
(c) More than once
(d) None
164.In what sequence the initialization , testing and execution of body is done in a
do-while loop
(a) Initialization, execution of body,testing
(b) execution of body,initialization,testing
( c ) Initialization,testing,execution of body
e None of the above
165.Which of the following statement is used to take the control to the beginning of
the loop?
(a)exit
(b)break
(c)continue (d)None
166. What is the continue statement used for ?
(a)To continue to the next line of code.
(b)To stop the current iteration and begin the next iteration from the
beginning.
( c)As an alternative to the else statement. (d) None of the above.
167. What is the output of the following program?
main()
{
printf("%d",-5/-4);
}
(a)1.25
(b)-1.25
(c)-1
(d)1
168. The left shift operator in C program is denoted by
(a) <<
(b)>>
(c)<
d .None of the Above
169. Relational operator in C compares two
(a) Operands
(b) operators
(c) Both A and B
(d) None of the
Above
170. Each C statement gets terminated with the notation
(a)$
(b) %
(c) ;
(d) None of the Above
171. Which of the following is an example of compounded assignment statement?
(a) a = 5
(b) a+=5
(c) a=b=c
(d) a=b
172. The operator & is used for ____
(a) Bitwise AND (b) Bitwise OR (c) (a) Logical AND
(d) (a) Logical OR
173. The equality operator is represented by
(a) :=
(b) .EQ.
(c) =
(d) ==
174. The bitwise AND operator is used for
(a) Masking
(b) Comparison (c) Division
(d) Shifting bits
175. Which operator has the highest priority?
(a) ++
(b)%
(c)+
(d)||
176. Which of the following is a ternary operator?
(a) ? :
(b) *
(c) sizeof
(d) ^
177. The operator + in a+=4 means
(a) a=a+4
(b) a+4=a
(c) a=4
(d) a=4+4
178. int a=10,b;
b=a++ + ++a;
printf("%d,%d,%d,%d",b,a++,a,++a);
SVCN , Kodavaluru
Page 48
II-BTech
(a)12,10,11,13
(b) 22,10,11,13
(c) 22,11,11,11 (d) 12,11,11,11 (e)
22,13,13,13
179. i nt x = 2 * 3 + 4 * 5;
What value will x contain in the sample code above?
(a) 22
(b) 26
(c) 46
(d)50
e .70
180. int x=3;
If(x==2);
X=0;
If(x==3)
X++;
else x += 2;
What value will x contain when the sample code above is executed?
(a)1
(b)2
(c)3
(d)4
(e)5
181. int x=5;
Int y=2;
Char op=*;
Switch(op)
{
Default: x+=1;
Case +: x+=y;
Case -:x-=y;
}
After the sample code above has been executed, what value will the variable x
contain?
(a)4
(b)5
(c) 6
(d)7
(e)8
182. int x=0;
for(; ;)
{
If(x++==4)
break;
continue;
}
printf(x=%d\n,x);
What will be printed when the sample code above is executed?
(a) x=0
(b) x=1
(c) x=4
(d) x=5
(e)
x=6
183. Which one of the following is NOT a valid C identifier?
(a) ___S
(b) 1___
(c)___1
(d)___
(e) S___
184. What number is equivalent to -4e3?
(a) -4000
(b)-400
(c)-40
(d) .004
(e) .0004
Question Bank
Page 49
II-BTech
6. What is token. Explain different c tokens.
7. What are bitwise operators. Write a program to demonstrate various bitwise
operators.
8. Explain about various looping statements in detail.
9. Explain different data types available in c with examples.
10. What is an identifier? What are the naming conventions used for identifiers in
c.
11. What is an expression? Explain different categories of expressions.
12. Explain with an example how an expression is evaluated.
13. Write about precedence and associativity of operators.
14. Explain about break, continue and goto statements with an example for
each.
15. What is the use of type conversion. Explain various ways of performing type
conversions.
16. What is a constant? Specify the use of symbolic constants. And also explain
various ways for defining symbolic constants.
17. Explain switch statement with an example.
18. What are Backslash Characters? List various backslash characters used.
Important Programs
1. Area and perimeter of a
9. Reverse of a number
circle ( By using a macro)
10. Fibonacci series
2. Finding the average of four
11. Factorial of a number
12. Number palindrome
subjects( By using type
13.
Write a program to find
casting)
sum of given series
3. Finding the roots of a
14.
sum=2+3/1!-6/2!+9/3!quadratic equation
4. Biggest of three numbers
12/4!.............
5. Program to demonstrate
15. To
perform
arithmetic
shifting operations
operations
using
switch
6. Finding the Grade of a
statement
student.
16. Program
to
illustrate
7. Sum of digits of a number
logical operators.
8. Armstrong number
SVCN , Kodavaluru
Page 50
17.