0% found this document useful (0 votes)
4 views9 pages

Java Programming Basics: Arrays, Control & Operators

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

Java Programming Basics: Arrays, Control & Operators

Some Java assignment questions
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA – ASSIGNMENT 2

Name: Muhammed Niyasudheen K N


College: College of Applied Science, Nattika
Submission Date: 10 September 2025
Index
1. What are Arrays? Explain its types with syntax and example

2. Explain Control Statements with syntax and example

3. Explain Iteration Statements with syntax and example

4. Explain Operators in Java

5. Explain Class, Object creation with sample example


1. What are Arrays? Explain its types with syntax and example
An array is a collection of elements of the same data type stored in contiguous memory
locations. Arrays in Java are used to store multiple values in a single variable instead of
declaring separate variables for each value.

Types of Arrays in Java:

1. Single-Dimensional Array (1D Array):


Syntax:
int[] arr = new int[5];
Example:
int[] numbers = {10, 20, 30, 40};

2. Multidimensional Array:
Syntax:
int[][] arr = new int[3][3];
Example:
int[][] matrix = { {1, 2}, {3, 4} };

Diagrams:

1D Array

2D Array
2. Explain Control Statements with syntax and example
Control statements are used to control the flow of execution of a program. There are three
main types:

1. if Statement:
Syntax:
if (condition) {
// code
}
Example:
if (num > 0) {
[Link]("Positive number");
}

2. if-else Statement:
Syntax:
if (condition) {
// code
} else {
// code
}
Example:
if (num % 2 == 0) {
[Link]("Even");
} else {
[Link]("Odd");
}

3. if-else-if Ladder:
Syntax:
if (condition1) {
// code
} else if (condition2) {
// code
} else {
// code
}
Example:
if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 75) {
[Link]("Grade B");
} else {
[Link]("Grade C");
}

4. switch Statement:
Syntax:
switch (variable) {
case value1:
// code
break;
default:
// code
}
Example:
switch (day) {
case 1:
[Link]("Sunday");
break;
case 2:
[Link]("Monday");
break;
default:
[Link]("Invalid");
}

Jump Statements:
5. break:
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break;
}
[Link](i);
}

6. continue:
Example:
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
[Link](i);
}
3. Explain Iteration Statements with syntax and example
Iteration statements allow repetitive execution of code blocks. Types are:

1. for loop:
Syntax:
for (initialization; condition; update) {
// code
}
Example:
for (int i = 0; i < 5; i++) {
[Link](i);
}

2. while loop:
Syntax:
while (condition) {
// code
}
Example:
int i = 0;
while (i < 5) {
[Link](i);
i++;
}

3. do-while loop:
Syntax:
do {
// code
} while (condition);
Example:
int i = 0;
do {
[Link](i);
i++;
} while (i < 5);
4. Explain Operators in Java
Operators in Java are special symbols that perform operations on operands. Types:

1. Arithmetic Operators: +, -, *, /, %

2. Relational Operators: ==, !=, >, <, >=, <=

3. Logical Operators: &&, ||, !

4. Assignment Operators: =, +=, -=, *=, /=

5. Unary Operators: +, -, ++, --

6. Bitwise Operators: &, |, ^, ~, <<, >>

7. Conditional (Ternary) Operator: condition ? value1 : value2

8. instanceof Operator: Used to check object type


5. Explain Class, Object creation with sample example
A class is a blueprint for objects. An object is an instance of a class.

Syntax for class:


class ClassName {
// fields
// methods
}

Example:
class Student {
String name;
int age;

void display() {
[Link](name + " " + age);
}

public static void main(String[] args) {


Student s = new Student();
[Link] = "John";
[Link] = 20;
[Link]();
}
}

Common questions

Powered by AI

Classes and objects form the basis of object-oriented programming in Java. A class is a blueprint for creating objects, defining properties (fields) and behaviors (methods) that the objects instantiated from the class can have. For instance, the Student class defines fields like name and age, and a method display() to show this data. An object is an instance of a class, representing specific entities with defined attributes and behaviors. In the example, creating an object such as Student s = new Student(); allows the developer to set values s.name = "John"; and invoke methods like s.display(); to perform operations on these values. This encapsulation promotes code reusability and organization .

Assignment operators in Java are used to assign values to variables; the most basic form is =, as in x = 10 assigning 10 to x. Compound assignment operators combine an operation with assignment, simplifying expressions and making code more concise. For example, x += 5 is shorthand for x = x + 5, adding 5 to x. These operators include +=, -=, *=, /=, and %=, and they not only reduce redundancy but also help streamline code where arithmetic operations and assignments are frequent, improving readability and decreasing susceptibility to errors in repeated code manipulation .

A single-dimensional array is a collection where elements are stored in a linear form, allowing access through a single index. Its syntax is int[] arr = new int[5]; For example, int[] numbers = {10, 20, 30, 40}; is a single-dimensional array storing integers. In contrast, a multidimensional array can be thought of as an array of arrays, and it is accessed using multiple indices. Its syntax is int[][] arr = new int[3][3]; For example, int[][] matrix = { {1, 2}, {3, 4} }; is a two-dimensional array storing integer pairs. Single-dimensional arrays are used for storing linear sequences, while multidimensional arrays are practical for representing more complex structures like matrices and tables .

The switch statement provides a more efficient and readable way of handling multiple conditional paths based on the value of a variable compared to using several if-else blocks. It is particularly useful when evaluating expressions that yield discrete values, such as integers, characters, or enumeration constants. Unlike if-else, which checks each condition sequentially, switch executes faster as it jumps directly to the matching case. For example, the switch handle: switch (day) { case 1: System.out.println("Sunday"); break; case 2: System.out.println("Monday"); break; default: System.out.println("Invalid"); } simplifies handling discrete days, compared to complex nested if-else statements .

A do-while loop is chosen over a while loop when the intention is to ensure the loop body executes at least once regardless of the condition state. This guarantee of single execution is necessary in scenarios where initial processing must occur before evaluating a condition. For example, when prompting user input until valid data is entered, do { System.out.println("Enter a positive number:"); n = scanner.nextInt(); } while (n <= 0); immediately prompts the user before checking whether n is positive, ensuring at least one prompt. The while loop lacks this guarantee, as it evaluates the condition before any execution, potentially preventing the loop from running if the condition is false at first .

The instanceof operator in Java is crucial for type-checking during runtime. It determines whether an object belongs to a specific class or interface, returning a boolean true or false. This is essential when dealing with polymorphism, where a reference could be of a superclass or interface type pointing to any object type. For instance, if obj instanceof Dog checks if obj is an instance of the Dog class or its subclasses. This prevents ClassCastException by ensuring type safety before performing cast operations. It is invaluable in scenarios where multiple classes share a common parent, and specific behavior is required for particular subclass types .

The for loop is used when the number of iterations is known before entering the loop, iterating with a control variable; for example, for (int i = 0; i < 5; i++) { System.out.println(i); } is optimal for fixed iterations. The while loop checks the condition before executing the loop block and is best for indefinite iterations where the exit condition is dynamic, for instance, int i = 0; while (i < 5) { System.out.println(i); i++; }. The do-while loop assures the block of code executes at least once before condition checking, useful when loop execution must occur regardless of initial condition, e.g., int i = 0; do { System.out.println(i); i++; } while (i < 5); making sure the block executes once even if i is not less than 5 .

Java's control statements empower developers to manage the execution flow and dictate the exact behavior of a program based on conditions and loops. The primary types include conditional statements (if, if-else, if-else-if ladder, and switch) and loop statements (for, while, and do-while). Conditional statements allow branching based on boolean expressions, providing different execution paths. For example, using if-else helps to make decisions like identification of even or odd numbers. Loops enable repetitive execution of a code block, optimized through statements like break and continue, to manage iteration neatly, making them essential for tasks such as iterating through arrays or handling repeated calculations .

Unary operators, such as ++ and --, are used to increment or decrement a value by one, often in loop control structures to modify the loop variable efficiently. For example, ++i increases the value of i by one. Unary operators also include + and - to indicate positive or negative values. Bitwise operators, like &, |, ^, ~, <<, and >>, perform operations on individual bits of integer types. They are used in scenarios that require manipulation of bits for performance reasons or in hardware interfacing, such as setting specific bits to configure a device register, e.g., int result = a & b combines bits of a and b using AND operation, while int shifted = a << 1 shifts bits of a one position to the left .

Logical operators, such as &&, ||, and !, are used to combine multiple boolean expressions or invert a boolean value. For instance, the expression (a > 0 && b < 5) evaluates to true only if both conditions are true. Relational operators, such as ==, !=, >, <, >=, and <=, compare two values or expressions. For example, num > 0 checks if num is greater than zero. In control flow statements, logical operators allow for more complex condition checks, while relational operators are used to compare values directly. For instance, if (num > 0 && num < 100) ensures num is within a certain range .

You might also like