KnowledgeBoat Logo
OPEN IN APP

Chapter 4 - Unit 1

Basics of Java Programming

Class 8 - APC Understanding Computer Studies with BlueJ



Choose the correct option

Question 1

Character literal is assigned to a:

  1. char variable
  2. char type literal
  3. string variable
  4. string literal

Answer

char variable

Reason — A character literal represents a single character enclosed in single quotes (' ') and it is assigned to char type variable.

Question 2

A character literal is enclosed in:

  1. ' '
  2. " "
  3. : :
  4. { }

Answer

' '

Reason — A character literal represents a single character enclosed in single quotes (' ').

Question 3

A set of characters is assigned to a:

  1. String variable
  2. Static variable
  3. Char variable
  4. None

Answer

String variable

Reason — A string data type represents a set of characters enclosed within double quotes. A set of characters is assigned to a String variable.

Question 4

The statement n += 4 is equivalent to:

  1. ++n
  2. n=n+4
  3. n+1
  4. None

Answer

n=n+4

Reason — Here, += is a Shorthand operator. It can be used to write a Java expression into short form provided same variables are used after and before the assignment sign (=).

Question 5

What will be the output of a++, if int a= -1;?

  1. 1
  2. -1
  3. 0
  4. none

Answer

-1

Reason — a++ uses an increment postfix operator which works on the principle- 'Action then change'. Hence, first the value of a is printed and then it is incremented.

Question 6

If int a=25, b=5, c=0; what value is stored in c when c = a%b?

  1. 5.0
  2. 5
  3. 0
  4. none

Answer

0

Reason — The expression uses mod operator (%) which returns the remainder after the operation. Here c = 25 % 5 which results in 0 as when 25 is divided by 5, we get the quotient as 5 and remainder as 0.

Question 7

What will be the result of m in the expression m*=8 if m=8; ?

  1. 8
  2. 64
  3. 16
  4. 88

Answer

64

Reason — m*=8 is equivalent to m=m*8 ⇒ m = 8 * 8 = 64.

Question 8

double c;
int x,y,z;
x=5; y=10; z=11;
c=x*y+z/2;
The value stored in c is:

  1. 55.0
  2. 55.5
  3. 55
  4. none

Answer

55.0

Reason — Putting the values of variables in the expression:
c = x * y + z / 2
c = 5 * 10 + 11 / 2
c = 50 + 5
c = 55.0 [∵ c is of double type, hence its value will be 55.0 not 55]

Question 9

int m,p; m=5; p=0; p= m-- + --m; The output will be:

  1. 11
  2. 10
  3. 8
  4. 12

Answer

8

Reason — Putting the values of variables in the expression:

Expression      Explanation
p = m-- + --mThis is the given expression. Initial value of m is 5.
p = 5 + --mAt this step, current value of m (which is 5) is used in the expression and then value of m is decremented to 4. Current value of m becomes 4 after this step.
p = 5 + 3--m first decrements the current value of m (which is 4) by 1 so m becomes 3. After that, this decremented value of 3 is used in the expression.
p = 8This is the final result of the expression.

Question 10

int a=7, p=0, q=0;
p= ++a + --a;
q-=p;

The output of q will be:

  1. 13
  2. 14
  3. 15
  4. -15

Answer

-15

Reason — Evaluating the given expressions:

Expression      Explanation
p = ++a + --aThis is the first expression. Initial value of a is 7 and p is 0.
p = 8 + --aAt this step, ++a first increments a by 1 so it becomes 8, then this incremented value is used in the expression.
p = 8 + 7 = 15At this step, current value of a (which is 8) is decremented by 1 and this decremented value of 7 is used in the expression.
    q-=p
⇒ q = q - p
⇒ q = 0 - 15
⇒ q = -15
Shorthand operator -= subtracts p from q and assigns the result back to q.

Question 11

What will be the output of 'a' and 'b' in the expression b = a++, if int a, b; a=10?

  1. 10,10
  2. 10,11
  3. 11,10
  4. 11,11

Answer

11,10

Reason — The given expression will result in b=10 and a=11 as the postfix operator will first use the value and then increment it. So, the output of a and b will be 11, 10.

State True or False

Question 1

Java is known as an Object-Oriented Programming (OOP) language.
True

Question 2

The comment line of the program is ignored during execution.
True

Question 3

The data type must be same as the value assigned to the variable.
True

Question 4

An integer value can be assigned to a double type variable.
True

Question 5

The letters 'ln' in the statement System.out.println() directs the cursor to move to the next line after displaying the value.
True

Question 6

The variables used in Java program are called reserved words.
False

Question 7

Literals remain unchanged throughout the execution of a program.
True

Question 8

When an increment or decrement operator is used before the operand, it is known as the postfix operator.
False

Write the Java expressions for the following

Question 1

ab + bc + ca

Answer

a * b + b * c + c * a

Question 2

a2 + ab - b2

Answer

a * a + a * b - b * b

Question 3

ut + 1/2at2

Answer

u * t + (1.0 / 2) * a * t * t

Question 4

(a+b)3

Answer

(a + b) * (a + b) * (a + b)

Question 5

2(lb + bh + lh)

Answer

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

Question 6

a2 + b2

Answer

a * a + b * b

Predict the output

Question 1

If m = 5 and n = 2, predict the output values of m and n, when:

(a) m -= n;

(b) n = m + m/n;

Answer

(a) m -= n;

Output
m = 3
n = 2
Explanation

    m -= n
⇒ m = m - n
⇒ m = 5 - 2
⇒ m = 3
Value of n is unchanged so it is 2.

(b) n = m + m/n;

Output
m = 5
n = 7
Explanation

    n = m + m/n;
⇒ n = 5 + 5/2;
⇒ n = 5 + 2;     [∵ 5/2 is integer division so result is 2]
⇒ n = 7
Value of m is unchanged so it is 5.

Question 2

What will be the output of the variable 'a'?

int a=0, b=10, c=40;
a= --b + (c++) + b;

Answer

Output
a = 58
Explanation

    a = --b + c++ + b
⇒ a = 9 + 40 + 9
⇒ a = 58

Question 3

What will be the output of the following, if x = 5 initially?

(a) 5 * ++x;

(b) 5 * x++;

Answer

(a) 5* ++x;

Output
30
Explanation

    5* ++x
⇒ 5* 6       [∵ ++x will first increment x to 6 and then use it in the expression]
⇒ 30

(b) 5* x++;

Output
25
Explanation

    5* x++
⇒ 5* 5       [∵ x++ will first use the current value of x in the expression which is 5. After that x is incremented to 6]
⇒ 25

Question 4

Evaluate the following expressions:

If the values of the variables are a = 2, b = 3, and c = 9.

(a) a - (b++) * (--c);

(b) a * (++b) % c;

Answer

(a) a - (b++) * (--c);

Output
-22
Explanation

    a - (b++) * (--c)
⇒ 2 - 3 * 8       [∵ b++ uses current value of b in expression and then increments it, --c decrements c to 8 and then uses this 8 in the expression]
⇒ 2 - 24
⇒ -22

(b) a * (++b) % c;

Output
8
Explanation

    a * (++b) % c
⇒ 2 * 4 % 9       [∵ ++b increments b to 4 then uses it in the expression]
⇒ 8 % 9
⇒ 8

Question 5

If a = 5, b = 9, calculate the value of a in the following expression:
a += (a ++) - (++ b) + a

Answer

Output
6
Explanation

    a += a++ - ++b + a
⇒ a = a + (a++ - ++b + a)
⇒ a = 5 + (5 - 10 + 6) [∵ a++ will first use current value of a then increment it to 6. ++b will increment b to 10 and use the incremented value. As a++ incremented a to 6 so the value of last a in the expression is 6]
⇒ a = 5 + 1
⇒ a = 6

Case-Study Based Question

Question 1

The logical operators are used in between two conditions, which results in either 'True' or 'False' depending on the outcome of different conditions. Java uses three logical operators viz. AND, OR and NOT. Your friend has created a Java snippet that contains some errors due to which he is not able to execute it.

The program snippet created by him is as shown below:

int p=11, q=12, r=15;

(a) System.out.println ((p==q) AND (q!=r));

(b) System.out.println(!(p=q));

(c) System.out.println (p!==q);

(d) System.out.println ((p!=q) OR (q!=r));

Refer to the above snippet and help him by detecting the errors so that the snippet may execute successfully.

Answer

(a) System.out.println ((p==q)&&(q!=r));

The symbol of AND operator (&&) should be used in the expression.

(b) System.out.println(!(p==q));

'=' is an assignment operator. Using it in the expression p=q will assign the value of q to p. We use equality operator '==' to compare the values.

(c) System.out.println (p!=q);

The not equal operator is written incorrectly. The correct not equal operator is !=.

(d) System.out.println((p!=q) || (q!=r));

The symbol of OR operator (||) should be used in the expression.

Answer the following questions

Question 1

Define a program.

Answer

A program is defined as a set of instructions or commands in a specific programming language that instructs the computer to perform a specific task.

Question 2

What are the characteristics of a program? Name them.

Answer

The characteristics of a program are as follows:

  1. Portable
  2. User-friendly
  3. Concise
  4. Usable
  5. Flexible

Question 3

Who developed Java?

Answer

James A. Gosling developed Java programming language in 1991.

Question 4

Java is case sensitive. Explain.

Answer

Java is case sensitive. It means that as a programming language, the uppercase and lowercase letters are distinguished by the language and treated differently.

Question 5

Mention at least four features of Java.

Answer

The four features of Java are as follows:

  1. Java programs are both compiled and interpreted.
  2. Java programs are platform independent.
  3. Java is based on the concepts of objects. Hence, it is also known as object-oriented programming (OOP) language.
  4. Java is case sensitive. As a programming language, the uppercase and lowercase letters are distinguished by the language.

Question 6

What is meant by Java keywords? Explain.

Answer

Java keywords or reserved words are the words which carry a special meaning for the system compiler. These words are basically used for writing a Java statement in the program. Such words cannot be used for naming a variable in the program.

Some keywords are case, switch, int, float etc.

Question 7

How are data types classified?

Answer

The Data Types in Java are classified as below:

How are data types classified? Basics of Java Programming, APC Understanding Computer Studies Solutions ICSE Class 8.

Question 8

Define the following:

(a) Variable

(b) Floating literal

(c) String literal

(d) Character literal

Answer

(a) Variable — A variable is a named memory location which stores a value. The value of a variable can change depending upon the circumstances and problems in a program.
For example, add, emp_name, etc.

(b) Floating literal — Floating literal or Real literal are the fractional numbers. They are also called floating-point constants because the placement of decimal point can be after any digit of the numbers. For example, 24.6, -42.514, 0.0072, etc.

(c) String literal — A string is a set of alphanumeric characters. It may be a word, a sentence or a paragraph enclosed in double quotes (" ").
For example, "Understanding Computer Studies", "Year 2021", "10% per annum", etc.

(d) Character literal — All letters in uppercase or lowercase, digits and special symbols can be termed as character literals. A character literal represents a single character enclosed in single quotes (' ').
For example, 'I', 'd', '3', '*', etc.

Question 9

Write down the data type of the following:

(a) Integer

(b) Long Integer

(c) A character

(d) A fractional number

Answer

(a) int

(b) long

(c) char

(d) float/double

Question 10

What is an operator? Name the different types of operators.

Answer

An operator is basically a symbol or a token which performs arithmetical, logical or relational operations to give meaningful results. The values which are involved in the operation, are termed as operands.

The three types of operators are as follows:

  1. Arithmetical Operators — They are further divided as follows:
    1. Unary Arithmetical Operators — This operator is further categorised in two ways:
      1. Unary plus (+) and Unary minus (-) Operators
      2. Unary Increment (++) and Decrement(--) Operators — They can be used as prefix operators or postfix operators.
    2. Binary Arithmetical Operators — For example, +, -, *, / etc.
  2. Relational Operators — For example, <, >, <>, != etc.
  3. Logical Operators — Logical operators are !(NOT), &&(AND) and ||(OR).

Question 11

State the difference between = and ==.

Answer

===
It is the assignment operator used for assigning a value to a variable.It is the equality operator used to check if a variable is equal to another variable or literal.
E.g. int a = 10; assigns 10 to variable a.E.g. if (a == 10) checks if variable a is equal to 10 or not.

Question 12

What is meant by a constant? Explain with an example.

Answer

Constants or literals are the constant values used in a Java program. While writing a program, you can use some values which remain fixed throughout the execution of the program. Such values are called literals or constants.

For example, consider the statement x = x + 20;
In this statement, 20 is a constant as 20 remains the same throughout the execution of the program.

Question 13

What are the rules to be followed while naming a variable in Java programming?

Answer

The rules to be followed while naming a variable in Java programming are as follows:

  1. A variable may have any number of characters.
  2. It may contain letters, digits and a underscore.
  3. It must not start with a digit or special character.
  4. It must not contain spaces, punctuation marks or any symbol.
  5. The underscore can be inserted in between the characters to separate the words, in case a variable includes multiple words.
  6. Variable name should be meaningful which specifies what it represents.

Question 14

Distinguish between the following:

(a) Source code and Object code

(b) Compiler and Interpreter

Answer

(a) The differences between source code and object code are as follows:

Source codeObject code
Source code is a set of statements in a High-Level programming language.Object code is a set of statements in Machine Language.
Source code is understood by human/programmer.Object code is understood by the processor.
Source code is not machine specific.It is machine specific.

(b) The differences between compiler and interpreter are as follows:

CompilerInterpreter
It converts the entire source program into the object program at once.It converts the source program into the object program, one line at a time.
It displays errors for the whole program together after compilation.It displays the errors, one line at a time, and only after correction, the control goes to the next line.

Question 15

Explain the following:

(a) Arithmetical operator

(b) Relational operator

(c) Logical operator

(d) Arithmetic expression

Answer

(a) Arithmetical operator — Arithmetical operators are used to perform mathematical operations on its operands. These calculations may include addition, subtraction, multiplication, division and modulus. Operands of arithmetic operators must be of numeric type. We can apply arithmetical operators like +, -, *, / and % respectively to carry out these calculations.
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.

Arithmetical operators can be categorised as follows:

  1. Unary Arithmetical Operators — This operator is further categorised in two ways:
    1. Unary plus (+) and Unary minus (-) Operators
    2. Unary Increment (++) and Decrement(--) Operators — They can be used as prefix operators or postfix operators.
  2. Binary Arithmetical Operators — For example, +, -, *, / etc.

(b) Relational operator — 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 — These operators are used in between two conditions and results in either 'True' or 'False' depending on the outcome of different conditions. Java uses logical operators AND (&&), OR (||) or NOT (!).
For example, if a=10, b=8, c=20; then, the result of (a>b)&&(c>b) will result in true as both the conditions are true.

(d) Arithmetic expression — An Arithmetic expression is an expression that contains variables, constants and arithmetical operators together to produce meaningful results.
For example, a + b * c - d is an arithmetic expression with variables a, b, c and d being of numeric type.

Question 16

Differentiate between the following:

(a) Arithmetical and Logical operator

(b) Logical AND (&&) and Logical OR (||)

(c) Prefix and Postfix operator

Answer

(a) Arithmetical and Logical operator

Arithmetical OperatorLogical Operator
Arithmetic operators are used to perform mathematical operations.Logical operators operate on boolean expressions to combine the results of these boolean expression into a single boolean value.
+, -, *, /, etc. are a few examples of Arithmetic operators.&&, ||, ! are a few examples of Logical Operators

(b) Logical AND (&&) and Logical OR (||)

Logical AND (&&)Logical OR (||)
The AND operator results in true, if both the conditional expressions are true.The OR operator results in true, if either of the two conditions is true, otherwise it will result in false.
For example, if a=8, b=13; the expression ((a > 10) && (b > 10)) will result in false.For example, if a=8, b=13; the expression ((a > 10) || (b > 10)) will result in true.

(c) Prefix and Postfix operator

Prefix operatorPostfix operator
When an increment or decrement operator is used before the operand, it is known as prefix operator.When an increment or decrement operator is used after the operand, it is known as the postfix operator.
Prefix operator works on the principle of 'Change before the action'.Postfix operator works on the principle of 'Change after the action'.
The value of a variable will change before the operation takes place.The operand will be affected after performing the operation.
Example:
int a = 99;
int b = ++a;
After the execution of these two statements, both a and b will have the value of 100.
Example:
int a = 99;
int b = a++;
After the execution of these two statements, a will have the value of 100 and b will have the value of 99.
PrevNext