0% found this document useful (0 votes)
154 views18 pages

Isc Xi CS Notes

computer science notes

Uploaded by

ksharada74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
154 views18 pages

Isc Xi CS Notes

computer science notes

Uploaded by

ksharada74
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Chapter 5

Concept of Data Types

Fill in the blanks with an appropriate word/words

Question 1

A character literal is enclosed in single quotes.

Question 2

A set of characters is assigned to a String.

Question 3

The ASCII codes of lowercase letters ranges from 97 to 122.

Question 4

A literal representing True or False comes under boolean data type.

Question 5

11.4f/2.0d is a floating type constant.

Question 6

Class is an example of non primitive data type.

Question 7

A set of characters used together under double quotes is called a string.

Question 8

A word used in a high level language that carries a special meaning for the system compiler is called a
keyword.

Question 9

A character literal is assigned to a char variable.

Question 10

In hierarchical arrangement, the higher most data type is double.


Write Short Answers

Question 1

What do you understand by data type?

Answer

Data types are used to identify the type of data a memory location can hold and the associated
operations of handling it.

Question 2

State two kinds of data types.

Answer

Two kinds of data types are:

1. Primitive Datatypes.
2. Non-Primitive Datatypes.

Question 3

What do you mean by a variable? Explain with an example.

Answer

A variable represents a memory location through a symbolic name which holds a known or unknown
value of a particular data type. This name of the variable is used in the program to refer to the stored
value.
Example:
int mathScore = 95;

Question 4

Define constant with an example.

Answer

The keyword final before a variable declaration makes it a constant. Its value can't be changed in the
program.
Example:
final int DAYS_IN_A_WEEK = 7;

Question 5

What do you understand by primitive and non primitive data types? Give two examples of each.

Answer

Primitive data types are the basic or fundamental data types used to declare a variable. Examples of
primitive data types in Java are byte, short, int, long, float, double, char, boolean.
A non-primitive data type is one that is derived from Primitive data types. A number of primitive data
types are used together to represent a non-primitive data type. Examples of non-primitive data types
in Java are Class and Array.

Question 6

What do you understand by Token? Name different types of tokens used in Java.

Answer

A token is the smallest element of a program that is meaningful to the compiler. The different types of
tokens in Java are:

1. Identifiers
2. Literals
3. Operators
4. Separators
5. Keywords

Question 7

What are the points to be taken care while assigning variables in Java programming?

Answer

1. Name of the variable should be a sequence of alphabets, digits, underscore and dollar sign
characters only.
2. It should not start with a digit.
3. It should not be a keyword or a boolean or null literal.

Question 8

Differentiate between:

(a) Integer and Floating constant

Answer

Integer Constant Floating Constant

Integer Constants represent whole number Floating Constants represent fractional numbers
values like 2, -16, 18246, 24041973, etc. like 3.14159, -14.08, 42.0, 675.238, etc.

Integer Constants are assigned to variables of Floating Constants are assigned to variables of
data type — byte, short, int, long, char data type — float, double
(b) Character and String constant

Answer

Character Constant String Constant

Character Constants are written by enclosing a String Constants are written by enclosing a set of
character within a pair of single quotes. characters within a pair of double quotes.

Character Constants are assigned to variables of String Constants are assigned to variables of type
type char. String.

(c) String and Boolean constant

Answer

String Constant Boolean Constant

String Constants are written by enclosing a A boolean constant can take only one of the two
set of characters within a pair of double boolean values represented by the words true or
quotes. false.

String Constants are assigned to variables of Boolean literals can only be assigned to variables
type String. declared as boolean

(d) Token and Identifier

Answer

Token Identifier

A token is the smallest element of a program Identifiers are used to name things like classes,
that is meaningful to the compiler. objects, variables, arrays, functions an so on.

(e) Separator and Punctuator

Answer

Separator Punctuator

Separators are used to separate the variables or the Punctuators are the punctuation signs used as
character. special characters in Java.

Comma, Braces, Curly Brackets, Square brackets Question mark, semi colon, dot are examples
are examples of Separators. of Punctuators.

Question 9

Write down the Data type of the following:


(a) Integer

Answer

int

(b) Long Integer

Answer

long

(c) A fractional number

Answer

double

(d) A special character

Answer

char

Question 10

What do you understand by Boolean type data ? Explain with an example.

Answer

A boolean data type is used to store one of the two boolean values — true or false. The size of boolean
data type is 8 bits or 1 byte.
Example:
boolean bTest = false;

Question 11

Define of the following terms with an example:

(a) Implicit type conversion

Answer

In implicit type conversion, the result of a mixed mode expression is obtained in the higher most data
type of the variables without any intervention by the user. Example:

int a = 10;
float b = 25.5f, c;
c = a + b;
(b) Explicit type conversion

Answer

In explicit type conversion, the data gets converted to a type as specified by the programmer. For
example:

int a = 10;
double b = 25.5;
float c = (float)(a + b);
(c) Literals

Answer

Any constant value which can be assigned to the variable is called as literal.

(d) Identifiers

Answer

Identifiers are used to name things like classes, objects, variables, arrays, functions an so on. They are
the symbolic representation by which a logical structure is identified.

Question 12

What is the significance of declaring a variable in Java?

Answer

Declaring a variable helps the Java compiler to know the type of value that we want to store in
the variable The compiler uses this information to allocate proper memory for storing the variable.

Question 13

What do you mean by initialization of a variable?

Answer

After we declare a variable, its value is unknown. Initialization of a variable means assigning a value
to the variable for the very first time after it is declared.

Question 14

In what way is static declaration different from dynamic declaration?

Answer

In static declaration, the initial value of the variable is provided as a literal at the time of declaration.
For example:

int mathScore = 100;


double p = 1.4142135;
char ch = 'A';
In dynamic declaration, the initial value of the variable is the result of an expression or the return
value of a method call. Dynamic declaration happens at runtime. For example:

int a = 4;
int b = Math.sqrt(a);

double x = 3.14159, y = 1.4142135;


double z = x + y;
Question 15

What do you mean by non-primitive data type? Give examples.

Answer

A non-primitive data type is one that is derived from Primitive data types. A number of primitive data
types are used together to represent a non-primitive data type. Examples of non-primitive data types
in Java are Class and Array.

Question 16

A non-primitive data type is also referred to as reference type. Why?

Answer

Unlike primitive data type, the allocation of non-primitive data type takes place in dynamic memory.
The accessing of non-primitive data types is based on their references (addresses). Hence, non-
primitive data types are also referred to as reference type.

Question 17

Explain the term type casting?

Answer

The process of converting one predefined type into another is called type casting.

Question 18

Perform the following:

(a) Assign the value of pie (3.142) to a variable with the requisite data type.

Answer

double pi = 3.142;

(b) Assign the value "TEST" to a variable with the requisite data type.

Answer

String str = "TEST";

Predict the return data type in the following expressions

Question 1

int p; double q;
r = p+q;

Answer

p+q
⇒ int + double
⇒ double
So the return data type of this expression is double.

Question 2

float m;
p = m/3*(Math.pow(4,3));

Answer

m/3*(Math.pow(4,3))
⇒ float / int * double
[∵Math.pow returns double]
⇒ float * double
⇒ double

So the return data type of this expression is double.

What are the resulting data types, if the following implicit


conversions are performed
int i; float f; double d; char c; byte b;

(a) i + c/b;

Answer

i + c/b;
⇒ int + char / byte
⇒ int + char
⇒ int

(b) f/d + c*f;

Answer

f/d + c*f;
⇒ float / double + char * float
⇒ double + float
⇒ double

(c) i + f - b*c;

Answer

i + f - b*c;
⇒ int + float - byte * char
⇒ int + float - char
⇒ float - char
⇒ float

(d) i/c + f/b;

Answer
i/c + f/b
⇒ int / char + float / byte
⇒ int + float
⇒ float

(e) i + f- c + b/d;

Answer

i + f- c + b/d;
⇒ int + float - char + byte / double
⇒ int + float - char + double
⇒ float - char + double
⇒ float + double
⇒ double

What are the resulting data types of the following explicit


conversions?
int i; float f; double d; short s; char c; byte b;

(a) (float) i/b + d;

Answer

(float) i/b + d
⇒ float / byte + double
⇒ float + double
⇒ double

(b) (int)f*d + c/s;

Answer

(int)f*d + c/s
⇒ int * double + char / short
⇒ double + short
⇒ double

(c) (char)i + f - b*d;

Answer

(char)i + f - b*d
⇒ char + float - byte * double
⇒ char + float - double
⇒ double

(d) (double) (f/i)*c + s;

Answer

(double) (f/i)*c + s
⇒ (double) (float / int) * char + short
⇒ double * char + short
⇒ double + short
⇒ double

(e) (char) d + b/i - f*s;

Answer

(char) d + b / i - f * s
⇒ char + byte / int - float * short
⇒ char + int - float
⇒ float
Chapter 6

Operators and Expressions


Fill in the blanks

Question 1

The statement n++ is equivalent to n = n + 1.

Question 2

If a=10;b=++a, then value of a and b will be 11 and 11.

Question 3

If a= -1 , then the output of a++ will be 0.

Question 4

If int a=43;int b=5;int c=0; and c = a%b; then the value of c will be 3.

Question 5

If int m=8; m*=8; then the output of m will be 64.

Question 6

If x=5;y=10;z=11; and c = x++ * 5 + --y * ++z; then the value of c will be 133.

Question 7

If m=5;p=0;and p = ++m + --m; the output will be 11.

Question 8

If int a=7;int p=0; and p = ++a + - -a; the value of p will be 16.

Question 9

The binary equivalent of (45) 10 is 101101.

Question 10

If a=0,b=1; then the output of bitwise OR will be 1.

Write the Java statements for the following

Question 1

p = a2 + bc
Answer

p=a*a+b*c

Question 2

m = a2 - b2 / (ab)

Answer

m = (a * a - b * b) / (a * b)

Question 3

s = ut + (1/2)at2

Answer

s = u * t + (1 / 2) * a * t * t

Question 4

f = uv / (u + v)

Answer

f = u * v / (u + v)

Question 5

x = -b + √(b2 - 4ac) / 2a

Answer

x = (-b + Math.sqrt(b*b - (4*a*c))) / (2 * a)

Question 6

y = 2(lb + bh + lh)

Answer

y = 2 * (l * b + b * h + l * h)

Question 7

c = a 2 + b2

Answer

c=a*a+b*b

Question 8

z = x3 + y3 - xy / z

Answer

z=x*x*x+y*y*y-x*y/z
Write short answers

Question 1

What is an operator?

Answer

An operator is a symbol or sign used to specify an operation to be performed in Java programming.

Question 2

What are the three main types of operators? Name them.

Answer

Three main types of operators are Arithmetical, Logical and Relational.

Question 3

What is a Truth Table?

Answer

A Truth Table is a tabular representation of the truth values of different propositions and the final
conclusion drawn after connecting them with the help of some connectives.

Question 4

What do you understand by Bitwise operator? Explain.

Answer

Operators which perform operations on bit level of the operands are referred to as Bitwise Operators.

Question 5

Explain with an example of each of the following operators:

(a) Arithmetic operator

Answer

Arithmetic operators are used to perform mathematical operations on its operands. Operands of
arithmetic operators must be of numeric type. A few arithmetic operators operate upon one operand.
They are called Unary Arithmetic operators. Other arithmetic operators operate upon two operands.
They are called Binary Arithmetic operators. As an example consider the below statement:
int a = 10 + 20;
Here, the addition arithmetic operator, represented by the symbol + will add 10 and 20. So
variable a will be 30.
(b) Relational operator

Answer

Relational operators are used to determine the relationship between the operands. Relational
operators compare their operands to check if the operands are equal to ( == ), not equal to ( != ), less
than ( < ), less than equal to ( <= ), greater than ( > ), greater than equal to ( >= ) each other. The result
of an operation involving relation operators is a boolean value — true or false.
Example:
int a = 8;
int b = 10;
boolean c = a < b;
Here, as a is less than b so the result of a < b is true. Hence, boolean variable c becomes true.
(c) Logical operator

Answer

Logical operators operate on boolean expressions to combine the results of these boolean expression
into a single boolean value.
Example:
int a = 7;
int b = 10;
boolean c = a < b && a % 2 == 0;
Here, the result of first boolean expression a < b is true and the result of second boolean
expression a % 2 is false. The logical AND operator ( && ) combines these true and false boolean
values and gives a resultant boolean value as false. So, boolean variable c becomes false.
(d) Ternary operator

Answer

condition ? expression 1 : expression 2


Ternary operator evaluates the condition. If the condition is true then result of ternary operator is the
value of expression 1. Otherwise the result is the value of expression 2.
Example:
boolean isLeapYear = true;
int febDays = isLeapYear ? 29 : 28;
Here, the ternary operator checks if the value of boolean variable isLeapYear is true or false. As it is
true, expression 1, which in this example is the value 29, is the result of the ternary operator. So, int
variable febDays becomes 29.

Question 6

Distinguish between :

(a) Unary and Binary arithmetic operator

Answer

Unary Arithmetic Operator Binary Arithmetic Operator

It operates on a single operand It operates on two operands

Increment (++) and Decrement (--) operators are Multiplication (*) and Division (/) are
examples of Unary Arithmetic Operators examples of Binary Arithmetic Operators
(b) Increment and Decrement operator

Answer

Increment operator Decrement operator

It increases the value of its operand by 1. It decreases the value of its operand by 1.

It represented as ++ It is represented as --

(c) Prefix and Postfix operator

Answer

Prefix Operator Postfix Operator

It works on the principle of CHANGE-


It works on the principle of USE-THEN-CHANGE.
THEN-USE.

It is written before the operand. It is written after the operand.

Example: Example:
int a = 99; int a = 99;
int b = ++a; int b = a++;
After the execution of these two statements, After the execution of these two statements, a will
both a and b will have the value of 100. have the value of 100 and b will have the value of 99.
(d) Operator and Expression

Answer

Operator Expression

An operator is a symbol or sign used to specify an An expression is a set of variables,


operation to be performed. constants and operators.

Expression is a combination of operators


Operator works on operands.
and operands.
Question 7

Define the following with the help of with table.

(a) Bitwise AND

Answer

This operator results in high (1) if both the operands are high, otherwise low (0).

Truth Table

a b a&b

0 0 0

0 1 0

1 0 0

1 1 1

(b) Bitwise NOT

Answer

This operator results in high (1) if bit operand is low (0) and vice-versa. Bitwise NOT is a unary
operator.

Truth Table

a !a

0 1

1 0
(c) Bitwise OR

Answer

Bitwise OR operator results in high (1), if either or all of its operands are high otherwise low (0).

Truth Table

a b a|b

0 0 0

0 1 1

1 0 1

1 1 1

(d) Bitwise XOR

Answer

This operator result is low (0) for the same values of the operands. The outcome is high (1) for
different values.

Truth Table

a b a^b

0 0 0

0 1 1

1 0 1

1 1 0

Question 8

If m = 5, n =2; what will be the output of m and n after execution?

(a) m -= n

Answer

m -= n
⇒m=m-n
⇒m=5-2
⇒m=3

(b) n = m + m/n;

Answer

n = m + m/n
⇒n=5+5/2
⇒n=5+2
⇒n=7

Question 9

What will be the output when the following statements are executed?

int v,s,n=550;
s = n + v > 1750? 400:200;
When,

(a) v = 500

Output

200

Explanation

n + v = 500 + 550 = 1050. As 1050 is less than 1750 so condition of ternary operator is false. Ternary
operator returns 200 as its output that gets assigned to s.

(b) v = 1500

Output

400

Explanation

n + v = 1500 + 550 = 2000. As 2000 is greater than 1750 so condition of ternary operator is true. Ternary
operator returns 400 as its output that gets assigned to s.

Question 10

If a = 0, b = 30, c = 40; then find the value of 'a' when:

a += --b + c++ + b;

Output

a = 98

Explanation

a = --b + c++ + b
⇒ a = 29 + 40 + 29
⇒ a = 98

You might also like