BCS306A Module 1 Question Answers
BCS306A Module 1 Question Answers
Key Characteristics:
Top-Down Approach: Programs are divided into small parts called functions.
No Access Specifiers: There are no access specifiers like private, public, or protected.
Less Secure: Procedural programming does not have a proper way of hiding data, making
it less secure.
No Inheritance: There is no concept of inheritance.
Function-Centric: Functions are more important than data.
Medium-Sized Programs: Suitable for designing medium-sized programs.
Procedure Abstraction: Uses the concept of procedure abstraction.
No Code Reusability: Code reusability is absent.
Object-Oriented Programming
Object-Oriented Programming (OOP) is based on the concept of objects that contain data in
the form of attributes and code in the form of methods. OOP models software design around
data or objects rather than functions and logic 2. Examples of OOP languages include Java,
C++, Python, and C#.
Key Characteristics:
Bottom-Up Approach: Programs are divided into small parts called objects.
Access Specifiers: Includes access specifiers like private, public, and protected.
More Secure: Provides data hiding, making it more secure.
Inheritance: Supports inheritance, allowing new objects to inherit characteristics from
existing ones.
Data-Centric: Data is more important than functions.
Large and Complex Programs: Suitable for designing large and complex programs.
Data Abstraction: Uses the concept of data abstraction.
Code Reusability: Code reusability is present due to inheritance.
OOPs provides data hiding whereas in Procedure-oriented programming language, global data
can be accessed from anywhere.
OOPs provides ability to simulate real-world event much more effectively. We can provide the
solution of real word problem if we are using the Object-Oriented Programming language.
1. Simple
Java has a clean and easy-to-understand syntax.
It removes complex features like pointers.
Automatic garbage collection simplifies memory management.
2. Object-Oriented
Everything in Java is treated as an object.
Supports core OOP principles: encapsulation, inheritance, polymorphism, and
abstraction.
3. Platform Independent
Java follows the Write Once, Run Anywhere (WORA) principle.
Code is compiled into bytecode, which can run on any system with a Java
Virtual Machine (JVM).
4. Portable
Bytecode is not dependent on any specific hardware or OS.
Java programs can run on multiple platforms without modification
5. Secure
Java provides built-in security features like bytecode verification and
sandboxing.
It avoids direct memory access, reducing vulnerabilities like buffer overflows.
6. Robust
Designed to handle errors gracefully, maintain stability under unexpected
conditions, and ensure the reliability of software applications.
7. Multithreaded
Java supports concurrent execution of two or more threads.
Ideal for applications like games, simulations, and real-time systems
8. High Performance
Though interpreted, Java uses Just-In-Time (JIT) compilers to improve
performance.
Efficient memory and thread management contribute to speed.
9. Distributed
Java supports distributed computing via technologies.
Useful for building networked applications.
10. Dynamic
Java supports dynamic loading of classes at runtime.
It can interact with native methods written in C or C++.
“Compile Once and Run Anywhere” in Java refers to its platform independence, made
possible by the Java Virtual Machine (JVM).
Java code, it is compiled into bytecode (.class files), not directly into machine-specific
instructions.
This bytecode is interpreted by the JVM, which acts as a bridge between the Java program and
the underlying hardware.
7. Explain two components of Java.
Java Development Kit (JDK) and Java Runtime Environment (JRE) are two essential
components in the Java
9. What is a keyword?
Keywords are the tokens which have special meaning in the programming language.
They are also called as reserved words.
Example – for, if, else, while, etc.
10. Discuss different data types in java along with its default values.
Data type defines what type of data is stored in a variable and amount of memory
allocated for the variable. Java is strongly typed language which means a variable must
be declared before being used. Java also checks for type compatibility during assignment
or parameter passing. Different data types in java are :
Variable named memory location to store a value that can be changed anywhere in the
program.
Example :
int c;
Constant is named memory location to store a value that cannot be changed anywhere in the
program. ‘final’ keyword is used in java to declare constants.
Literals are fixed values in the program. Java supports several types of literals, each
corresponding to a data type:
Integer Literals
Floating-Point Literals
Character Literals
String Literals
Null Literal
Represents the absence of a value for reference types.
String str = null;
An identifier is the name used to identify elements in your Java program—like variables,
arrays, methods, classes, interfaces, and more.
Comments are used to make code more readable and understandable. They are ignored by the
compiler, meaning they don’t affect how the program runs.
Single-line Comment
Begins with //
Used for brief explanations or notes
Multi-line Comment
Starts with /* and ends with */
Used for longer explanations or to temporarily disable blocks of code
In Java, separators (also called punctuators) are special symbols used to structure the
code and have special meaning.
Example :
Semicolon (;) – used to end a statement
Braces ({ } ) – Defines blocks of code
Comma , - used to separate statements, separate arguments in functions, etc.
17.What is whitespace character?
Whitespace characters are considered non-printable characters and they don’t produce a
visible symbol when displayed—they affect layout and spacing rather than content.
18. Explain type casting and type conversion in java with example?
Type casting and type conversion are techniques used to convert a variable of one data
type to another data type.
Type Conversion Automatically converts a smaller type Java compiler int → long → float →
(Widening) to a larger type (implicit) double
Type Casting Manually converts a larger type to a Programmer double → float → long
(Narrowing) smaller type (explicit) → int → short → byte
int a = 10;
double b = a; // int is automatically converted to double
System.out.println(b); // Output: 10.0
double x = 10.75;
int y = (int) x; // Explicit cast from double to int
System.out.println(y); // Output: 10
When evaluating an expression, Java automatically promotes operands to the largest data
type involved in the operation. This is called automatic type conversion. Automatic type
conversion is done only when following conditions are met.
Example :
byte b = 10;
short s = 20;
int result = b + s; // both b and s are promoted to int
System.out.println(result); // Output: 30
int i = 5;
float f = 6.5f;
float result = i + f; // i is promoted to float
System.out.println(result); // Output: 11.5
20. List the short circuit operators and show concept using few examples.
short-circuit operators are logical operators that skip evaluating the second operand if
the result can be determined from the first operand alone. This improves performance and
avoids unnecessary computation—or even runtime errors.
int x = 5;
if (x > 0 && x < 10) {
System.out.println("x is between 1 and 9");
}
If x > 0 is false, Java won’t check x < 10 because the whole condition can't be
true.
int y = 15;
if (y < 10 || y > 12) {
System.out.println("y is either less than 10 or greater than 12");
}
If y < 10 is true, Java won’t check y > 12 because the condition is already
satisfied.
21. List different types of operators in Java.
Operators are symbols used for calculation, comparison and joining expression.
1. Arithmetic Operators
● +: Addition
● -: Subtraction
● *: Multiplication
● /: Division
● %: Modulus (remainder)
2. Relational Operators
● ==: Equal to
● !=: Not equal to
● >: Greater than
● <: Less than
● >=: Greater than or equal to
● <=: Less than or equal to
3. Logical Operators
● &&: Logical AND
● ||: Logical OR
● !: Logical NOT
4. Bitwise Operators
● &: Bitwise AND
● |: Bitwise OR
● ^: Bitwise XOR (exclusive OR)
● ~: Bitwise NOT (complement)
● <<: Left shift
● >>: Signed right shift
● >>>: Unsigned right shift
5. Unary Operators
+ Unary plus
- Unary minus
++ Increment operator
-- Decrement operator
! Unary Not (logical)
6. Ternary Operator
?: ?:
Operator precedence in Java determines the order in which operators are evaluated in
expressions. When multiple operators appear in a single expression, those with higher
precedence are evaluated first. If operators have the same precedence, their
associativity (left-to-right or right-to-left) decides the evaluation order.
Precedence Level Operators Associativity
Brackets () (parentheses), [], method calls Left to Right
Unary ++, --, + (unary), - (unary), !, ~ Right to Left
*, /, % Left to Right
Arithmetic
+, - Left to Right
Shift <<, >>, >>> Left to Right
<, >, <=, >= Left to Right
Relational
==, != Left to Right
& Left to Right
Bitwise ^ Left to Right
| Left to Right
&& Left to Right
Logical
|| Left to Right
Ternary ?: Right to Left
Assignment =, +=, -=, etc. Right to Left
Example 1:
int a = 10, b = 5, c = 2;
boolean result = a + b * c > 20 && b - c < 5;
System.out.println(result); // Output: true
Example 2:
int x = 8, y = 3;
int result = (x > y) ? x + y * 2 : x - y * 2;
System.out.println(result); // Output: 14
Example 3:
int a = 5, b = 3;
a += b & 2 | 1;
System.out.println(a); // Output: 7
Example 4:
int a = 4, b = 6, c = 5;
String result = (a > b) ? "A is greater" : (b > c ? "B is greatest" : "C is greatest");
System.out.println(result); // Output: B is greatest
In Java >>> is called the unsigned right shift operator. It’s used to shift bits to the right
without preserving the sign bit, which makes it different from the regular >> operator
which is signed right shift operator.
Operator Name Sign Bit Preserved? Leftmost Bits Filled With
>> Signed right shift Yes Sign bit (0 or 1)
>>> Unsigned right shift No Always 0
For positive numbers, >> and >>> behave the same. The difference only shows up when
the number is negative, because >> preserves the sign bit while >>> does not.
Example :
int x = 8;
int y = 8;
System.out.println(x>>2);
System.out.println(y>>>2);
int a = -40;
int b = -40;
System.out.println( a>>2);
System.out.println(b>>>2);
Output :
2
2
-10
1073741814
The ternary operator in Java is a shorthand for an if-else statement. It makes code more
compact and readable for simple conditions.
Syntax:
condition ? expression1 : expression2
Example :
Benefits :
Example :
a += 5; // a = a + 5 → a becomes 15
a -= 3; // a = a - 3 → a becomes 12
a *= 2; // a = a * 2 → a becomes 24
a /= 4; // a = a / 4 → a becomes 6
a %= 5; // a = a % 5 → a becomes 1
26. List and explain control statements in Java with programming example.
A program is normally executed sequentially which means statements are executed one after
other. All problems cannot be solved using this approach. Few problems may require few
statements to be skipped or repeated again and again. Control structures are used to alter
sequential flow of execution.
if syntax
if (expression) {
// statements --- these statements are executed when above expression is true and
And not executed if above expression is evaluated to true
}
Example :
if (number % 2 == 0) {
System.out.println("The number is even.");
}
if-else syntax
if (expression) {
// statements -- these statements are executed when above expression is true
}
else {
// statements - these statements are executed when above expression is false
}
Example :
if-else-if syntax
if (expression1)
{
// statements -- these statements are executed when above expression1 is true
}
else if(expression2)
{
// statements -- these statements are executed when above expression2 is true
}
else if (expression3)
{
// statements -- these statements are executed when above expression3 is true
}
.
.
else
{
// statements -- these statements are executed when all above expressions are
false
}
Example :
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
switch Statement
In Java, the if..else..if ladder executes only one block of code among many blocks. The
switch statement can be a substitute for long if..else..if ladders which generally makes
code more readable. Switch can only test for equality.
switch (variable/expression) {
case value1:
// statements
break;
case value2:
// statements
break;
.. .. ...
.. .. ...
default:
// statements
}
Example :
switch (day) {
case 1:
System.out.println("It's Monday. Back to work!");
break;
case 2:
System.out.println("It's Tuesday. Keep going!");
break;
case 3:
System.out.println("It's Wednesday. Halfway there!");
break;
case 4:
System.out.println("It's Thursday. Almost the weekend!");
break;
case 5:
System.out.println("It's Friday. Weekend vibes!");
break;
case 6:
System.out.println("It's Saturday. Time to relax!");
break;
case 7:
System.out.println("It's Sunday. Recharge for the week!");
break;
default:
System.out.println("Invalid day number.");
}
The switch statement evaluates it's expression (mostly variable) and compares with
values (can be expression) of each case label. The switch statement executes all
statements of the matching case label. Notice, the use of break statement. This statement
terminates the execution of switch statement. The break statements are important because
if they are not used, all statements after the matching case label are executed in sequence
until the end of switch statement.
If the test expression is never false, for loop will run forever. This is called infinite for loop.
Example :
while (testExpression) {
// codes inside body of while loop
}
If the test expression is evaluated to true, statements inside the while loop are executed.
then, the test expression is evaluated again.
This process goes on until the test expression is evaluated to false.
If the test expression is evaluated to false, while loop is terminated.
Example :
int i = 1;
while (i <= 5) {
System.out.println("Number: " + i);
i++;
}
The do...while loop is similar to while loop with one key difference. The body of
do...while loop is executed for once before the test expression is checked.
do {
// codes inside body of do while loop
} while (testExpression);
The body of do...while loop is executed once (before checking the test expression). Only
then, the test expression is checked.
If the test expression is evaluated to true, codes inside the body of the loop are executed,
and the test expression is evaluated again. This process goes on until the test expression is
evaluated to false.
When the test expression is false, the do..while loop terminates.
Example :
int i = 1;
do {
System.out.println("Number: " + i);
i++;
} while (i <= 5);
for-each loop
for-each loop in java lets looping through of each element in a collection without needing
an index. It’s clean, readable, and avoids common mistakes like off-by-one errors.
Syntax:
for (type variable : collection) {
// code to execute with each element
}
Example :
int values[] = {14,17,45};
Output :
14
17
45
27. Explain about local variable type inference and with example.
Local variable type reference in a feature in java that allows type of a local variable to be
determined from the type of its initializer. This is achieved using keyword ‘var’.
Example :
class TypeInferenceDemo {
public static void main(String args[])
{
var x = 10.0f;
System.out.println(((Object) x).getClass().getName());
var y = 10.0;
System.out.println(((Object) y).getClass().getName());
}
}
Output :
java.lang.Float ---- x is initialized with float value and hence datatype of x is float
java.lang.Double ---- y is initialized with double value and hence datatype of x is float
Restrictions in usage of ‘var’ :
try {
int result = 10 / 0;
} catch (var e) { // invalid
System.out.println("Exception caught: " + e.getMessage());
}
jump statements are used to transfer control to another part of the program. They’re
especially useful for altering the normal flow of loops. Different jump statements in java
are :
Example (break):
Example (continue):
for (int i = 0; i < 5; i++) {
if (i == 3) {
continue; // skips following statement when i is 3
}
System.out.println(i);
}
Output of above code snippet:
0
1
2
4
return statement:
The return statement in Java is used to exit from a method and optionally send a value
back to the caller. It’s a fundamental part of how methods communicate results.
return statement -
Syntax :
return value; // For methods that return something
return; // For void methods
Rules to Remember
The return type in the method signature must match the type of value returned.
You must use return in non-void methods.
return can be used to exit early from a method conditionally.
There can be multiple return statements in a method.
Block is set of statements enclosed between curly braces ({}) and block defines the scope
of a variable. There are two types of scopes : class scope and method scope.
Method scope:
Method’s scope ends with its closing curly brace. There can be n number of scopes or
blocks inside method scope.
A variable declared inside a scope or block is visible to all inner scopes or blocks.
Variables are created while entering the scope and destroyed while leaving the
scope. Lifetime of a variable is confined to a scope.
Variable in the outer scope and inner scope cannot have same name.
Example :
class scopeDemo {
public static void main(String args[])
{
int i =10;
{
//int i = 20; // invalid
int j = 30;
i = 40 ; // variables of outer scope can be accessed and modified
in inner scope
System.out.println("i "+ i); // outer scope variables are visible
in inner scope
System.out.println("j "+ j);
}
// System.out.println("j "+ j); // variable j not visible here as it is
declared in inner scope
}
}
Output :
i 40
j 30
Normally, break only exits the innermost block. Using a labeled break, one can exit from
a block which is labeled.
Example:
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
i = 2, j = 2
Breaking out of outer loop
Loop exited
Array is a non-primitive datatype and can store group of values of same data type. Each
value in an array is accessed using index which starts from 0;
Example of 1D array:
Syntax of 2D Array:
Programs