Java Module1 - QB
Java Module1 - QB
1. Explain Java Buzzwords <OR> Explain features of Java that makes it different
from other languages <OR> Write a key advantage of Java.
The 11 Buzzwords are as mentioned below
Simple:
Java is easy to learn and use. It removes complex features like pointers, header files,
operator overloading, structures, and unions found in C/C++.
Secure:
Java provides a secure environment with built-in security features that help create virus-
free and tamper-free applications.
Portable:
Java programs are platform-independent. Code written once can run on any machine
— “Write Once, Run Anywhere.”
Object-Oriented:
Java follows object-oriented principles, focusing on objects and interfaces. Everything
is an object except primitive data types for better performance.
Robust:
Java has strong memory management, automatic garbage collection, and exception
handling, reducing runtime errors and memory corruption.
Multithreaded:
Java allows multiple threads to run simultaneously, enabling efficient multitasking
within a program.
Architectural Neutral:
Java generates architecture-independent bytecode that can run on any processor with a
Java Runtime Environment.
Interpreted & High Performance:
Java bytecode is interpreted and optimized using Just-In-Time (JIT) compilation for
better performance.
Distributed:
Java supports network-based applications using standard protocols like HTTP, FTP,
and features such as RMI.
Dynamic:
Java is more dynamic than C/C++ and is designed to adapt to changing environments.
It supports runtime type information, making programs flexible and extensible.
1. Whitespaces –
Java is a free-form language. This means that you do not need to follow any special
indentation rules. In Java, whitespaces include space, tab or newline.
2. Identifiers –
Identifiers are used to name classes, variables and methods. An identifier may be any
descriptive sequence of uppercase and lower case letters, numbers. Java is a case-
sensitive, so VALUE is different from value.
3. Literals –
A constant value in Java is created by using a literal representation for it. For
example, 100 is an integer literal, 98.6 is a floating-point literal, ‘N’ is a character
literal, and “Hello World” is a string literal.
4. Comments –
Java supports three types of comments:
• Single Line Comment – e.g., // this is a single line comment
• Multiline Comment – e.g., /* this is a Multiline comment */
• Documentation Comment – this type of comment is used to produce an HTML file
that documents your program. The documentation comment begins with /** and ends
with */.
5. Separators –
In Java, the most commonly used separator is semicolon ; – used to terminate
statements. The various separators are listed below,
Sl.
Symbol Name Purpose
No.
Used to contain parameter
list in method invocation,
for defining precedence in
1 () Parentheses expressions, to contain
expressions in control
statements, and for
surrounding cast types.
Used to define block of
code, for classes, methods
2 {} Braces and local scope, and
values of automatically
initialized arrays.
Sl.
Symbol Name Purpose
No.
Used to declare array
3 [] Brackets types and dereferencing
array value.
4 ; Semicolon Terminates statements.
Separates consecutive
5 , Comma identifiers in a variable
declaration.
Used to separate package
names, and to separate
6 . Period
variables and methods
from reference variables.
6. Java Keywords:
Java has 50 predefined keywords that cannot be used as identifiers (names of
variables, classes, or methods). Additionally, true, false, and null are data literals
and also cannot be used as identifiers.
4. Discuss the different data types supported by java along with the default values
and literals
Java defines eight primitive types of data: byte, short, int, long, float, double, char
and boolean. As shown in above figure
Integer Types: are signed, positive and negative values. Java does not support unsigned
values. Integer types hold the whole numbers, they cannot hold real numbers. There are
4 datatypes related to Integer group such as- byte, short, int, long. The size (width) of
Integer group is as shown below,
Floating-Point Numbers (Real Types): can hold real numbers, there are 2 datatypes
related to floating-point numbers, such as, float and double. ‘float’ stores real numbers
using single precision, whereas, ‘double’ stores real numbers using double precision.
The size of these datatypes are shown below,
Character Types: in Java is used to store characters. It occupies 2 bytes in the memory
unit and uses Unicode (ASCII + Other character set) to represent characters. The default
value for char is ‘space’.
Boolean: is used to deal with the logical values. It occupies 1 byte in the memory with
default value as ‘false’. It can have one of the two values namely ‘true’ or ‘false’. This
is the type returned by all relational operators.
• A block defines a scope, and variables declared within it are accessible only
inside that block.
class Scope
{
public static void main(String args[])
{
int x; // known to all code within main x = 10; if(x == 10) // start new scope
{
int y = 20; // known only to this block
// x and y both known here.
[Link]("x and y: " + x + " " + y); x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here. [Link]("x is " + x);
}
}
6. What do you mean by type conversion and type casting? Give examples.
The process of converting one datatype value to another datatype value is termed as
Type Casting. There are two types of type casting:
1. Implicit Casting (Widening Conversion) – Converting smaller datatype value to
bigger datatype value.
This type of conversion happens in following two conditions,
• The two types are compatible.
• The destination type is larger than the source type.
When these two conditions are met, a widening conversion takes place. This happens
automatically. There is no loss of data in implicit conversion.
public class Test
{
public static void main(String[] args)
{
int i = 100;
long l = i; //no explicit type casting required
[Link]("Int value "+i);
[Link]("Long value "+l);
}
}
class BasicMath {
public static void main(String args[]) {
[Link]("Arithmetic Operators Demo");
int a = 10;
int b = 20;
int c = a + b;
int d = a - b;
int e = a * b;
int f = b / a;
int g = b % a;
[Link]("Value of Addition = " + c);
[Link]("Value of Subtraction = " + d);
[Link]("Value of Multiplication = " + e);
[Link]("Value of Division = " + f);
[Link]("Value of Modulus = " + g);
String s1 = "10";
String s2 = "20";
String s3 = s1 + s2; // + performs concatenation
[Link]("Value of s3 is " + s3);
}}
class BitwiseDemo {
public static void main(String[] args)
{
[Link]("Bitwise Operators Demo");
byte a = 5;
int b = a >> 1; // Right shifting a value by 1 is same as dividing the value by 2
int c = a << 1; // Left shifting a value by 1 is same as multiplying the value by 2
[Link]("Value of a is " + a);
[Link]("Value of b is " + b);
[Link]("Value of c is " + c);
}
}
iii. Relational operators:
• Relational operators compare two operands to determine their relationship.
• They are binary operators and return a Boolean value (true or false).
• They are commonly used in loop and if statements for decision making.
class Demo{
public static void main(String [ ] args){
[Link]("Relational Operators Demo");
int a = 4;
int b = 1;
boolean c = a < b;
boolean d = a > b;
boolean e = (a == b);
boolean f = (a != b);
[Link]("Value of c = " + c);
[Link]("Value of d = " + d);
[Link]("Value of e = " + e);
[Link]("Value of f = " + f);
}
}
a=b=c=9;
[Link](str);
10. With a java program, illustrate the use of ternary operator to find the greatest of
three numbers.
class GreatestOfThree {
public static void main(String[] args) {
int a = 25;
int b = 40;
int c = 30;
// Using nested ternary operator
int greatest = (a > b)
? (a > c ? a : c)
: (b > c ? b : c);
[Link]("Numbers: " + a + ", " + b + ", " + c);
[Link]("Greatest number is: " + greatest);
}}
11. Develop a Java program to add two matrices using command line argument.
Study lab program
12. List and explain control statements in Java with programming example.
Java supports three types of control statements:
• Selection statement
• Iteration statement
• Jump statement
1. Selection statement:
• Java has two selection statements: if and switch.
• They control program flow based on conditions.
Types of if statements:
i. Simple if
ii. if-else
iii. if-else-if ladder
iv. Nested if-else
i. Simple if:
The general form of if statement is:
if (condition)
{
Statement1;
}
else
{
Statement2;
}
ii. if-else:
General form:
if (condition1)
{
if (condition2)
Statement1;
else
Statement2;
}
else
Statement3;
int a = 10;
int b = 25;
int c = 15;
switch (expression) {
case value1:
// statements
break;
case value2:
// statements
break;
case value3:
// statements
break;
default:
// default statements
}
2. Iteration statements:
Java supports 4 types of iterative statements,
• While loop
• Do-while loop
• For loop
• Enhanced for loop(for each loop)
i. While loop:
• It is a fundamental loop statement.
• It repeats a statement or block of statements as long as the condition is
true.
• The condition is checked before executing the loop body.
while (condition) {
// body of loop
}
Example:
public class WhileExample {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
[Link](i);
i++;
}
}
}
ii. do-while Loop:
do {
// body of loop
} while (condition);
Example:
int i = 1;
do {
[Link](i);
i++;
} while (i <= 5);
}
}
iii. For loop:
• The for loop is a looping statement used to repeat a block of code a fixed
number of times.
• It is commonly used when the number of iterations is known.
• It checks the condition before executing the loop body.
[Link](i);
// body of loop
}
Example:
int[] arr = {10, 20, 30};
for (int num : arr) {
[Link](num);
}
3. Jump statement:
i. Break:
The break statement has three uses:
• It terminates a statement sequence in a switch statement.
• It is used to exit a loop (for, while, do-while).
• It can be used as a labeled break (similar to goto) to exit from a specific block.
public class BreakExample {
public static void main(String[] args) {
if (i == 3)
continue;
[Link](i);
}
}
}
13. Develop a java program to demonstrate the working of for each version of for
loop. Initialize the 2D array with values and print them using for each.
public class ForEachDemo {
public static void main(String[] args) {
int[][] arr = {
{1, 2},
{3, 4}
};
14. Write a Java program with a method to check whether a given number is prime
or not.
public class PrimeNumber {
if (num <= 1) {
return false;
}
return true;
}
if (isPrime(number)) {
[Link](number + " is a Prime number");
} else {
[Link](number + " is not a Prime number");
}
}
}
15. Write a Java program to perform linear search on an array elements accepted
from keyboard and key element also accepted from key board.
import [Link];
// Linear Search
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
[Link]("Element found at position " + (i + 1));
found = true;
break;
}
}
if (!found) {
[Link]("Element not found");
}
[Link]();
}
}
16. Write a Java program to sort the elements using for loop.
public class SortArray {
public static void main(String[] args) {
int temp;