solution of java question paper1
solution of java question paper1
Answer: a) 3
Explanation: In Java, when both operands of a division are integers, integer division is
performed, which means that the fractional part is discarded. So, 10 / 3 equals 3.
Answer: d) 1
Answer: c) then
Explanation: "then" is not a recognized keyword in Java. Other options like if, switch, and
class are indeed Java keywords.
Answer: c) Semicolon
Explanation: In Java, every statement must end with a semicolon (;). This tells the compiler
that the statement is complete.
Answer: b) Unary
Answer: c) 1995
Answer: b) .java
Answer: Answer not provided in the options; however, the correct answer is: Sun
Microsystems
Answer: The break statement is used to exit from a loop or a switch statement.
Answer: A constant is a fixed value that cannot be altered during the execution of a program.
Answer:
Answer: An exception is an event that disrupts the normal flow of a program's execution.
Answer: An object is an instance of a class that contains data (attributes) and methods that
operate on that data.
Answer: The dot (.) operator in Java is used to access members of a class or interface.
Q.20 Define Polymorphism.
Example:
try {
int result = 10 / 0; // This will throw an ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Cannot divide by zero!");
} finally {
System.out.println("Execution completed.");
}
Answer:
While Loop:
Syntax: while (condition) { // code }
The condition is checked before executing the loop body.
The loop may not execute at all if the condition is false.
Do-While Loop:
Syntax: do { // code } while (condition);
The loop body executes at least once because the condition is checked after the loop
body runs.
Example:
// While Loop Example
int count = 0;
while (count < 5) {
System.out.println(count);
count++;
}
Summary:
Use a while loop when you want to check the condition before executing, and a do-while
loop when you need at least one execution of the loop regardless of the condition.
1. Platform Independence: JAVA code is compiled into bytecode, which can be run on
any platform with a Java Virtual Machine (JVM). This allows for the "write once, run
anywhere" (WORA) capability.
2. Object-Oriented: Java follows the principles of Object-Oriented Programming
(OOP), including encapsulation, inheritance, polymorphism, and abstraction,
promoting code reuse and modularity.
3. Robustness: Java provides strong memory management features with automatic
garbage collection, exception handling, and type checking during compile-time,
reducing runtime errors.
4. Security: Java includes several security features, such as bytecode verification and
the security manager, which restricts access to system resources, ensuring safer
execution of Java applications.
5. Multi-threading: Java supports multi-threading, allowing multiple threads to run
simultaneously, which enhances the performance of applications and improves
responsiveness.
The if statement in Java is a conditional statement that executes a block of code based on
whether the specified condition evaluates to true.
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
Method overloading is a feature that allows a class to have multiple methods with the same
name but different parameter lists (signature), helping in code readability and reusability.
Example:
class MathOperations {
// Method to add two integers
int add(int a, int b) {
return a + b;
}
In this example, the add method is overloaded with different parameter types and numbers.
Example:
class Animal {
void eat() {
System.out.println("Eating...");
}
}
In this example, Dog inherits the eat method from the Animal class, demonstrating single
inheritance.
Comments in Java are used to explain the code and make it more readable. They can be
single-line or multi-line.
1. Single-line comment: Starts with // and continues to the end of the line.
/* This is a
multi-line comment */
3. Documentation comment: Starts with /** and is used for generating documentation.
/**
* This is a documentation comment
* documentation for the method
*/
Class: A class is a blueprint or template that defines the properties (attributes) and behaviors
(methods) of objects. It represents a category of objects.
Object: An object is an instance of a class. It is a concrete entity that has state and behavior.
Objects hold data (attributes) and methods to perform actions.
Example:
class Car {
String color;
void drive() {
System.out.println("Driving...");
}
}
In this example, Car is the class, and myCar is an object of that class.
Q29: Define function. Explain its types
A function is a block of code that performs a specific task, has a name, and can return a
value. Functions can take parameters and can be called multiple times from different parts of
a program.
Types of Functions:
// User-defined function
int add(int a, int b) {
return a + b;
}
The for loop is a control structure that allows repeated execution of a block of code for a
specified number of iterations. It consists of three parts: initialization, condition, and
increment/decrement.
Syntax:
Example:
Here’s a simple Java program to calculate and print the factorial of a number using recursion.
import java.util.Scanner;
In summary, OOP is more suited for larger systems while POP is simpler for smaller, more
straightforward tasks.
Example:
class Car {
String color;
// Constructor
Car(String c) {
color = c;
}
void display() {
System.out.println("Car color is: " + color);
}
}
package myPackage;
import java.util.Scanner;
4. **Main
Example:
class Dog {
String breed; // attribute
2. Encapsulation:
Example:
class Account {
private double balance; // private attribute
3. Inheritance:
o Inheritance is a mechanism wherein one class can inherit the fields and
methods of another class. This promotes code reuse.
o The class that inherits is called the subclass (or derived class), and the class
from which it inherits is called the superclass (or base class).
Example:
class Animal {
void eat() {
System.out.println("Eating");
}
}
4. Polymorphism:
Example:
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
5. Abstraction:
o Abstraction is the concept of hiding complex realities while exposing only the
necessary parts. It allows defining abstract classes or interfaces that can be
implemented by subclasses.
Example:
Answer: Java provides several types of operators that can be used to perform
operations on variables and values:
5. Arithmetic Operators:
o Used for basic mathematical operations.
o Examples: +, -, *, /, and % (modulus).
2. Relational Operators:
if (a > b) {
// true if 'a' is greater than 'b'
}
3. Logical Operators:
4. Bitwise Operators:
5. Assignment Operators:
a += b; // equivalent to a = a + b
7. Instanceof Operator:
Answer: Arrays in Java are used to store multiple values of the same type in a single
variable. They can be categorized as follows:
7. Single-Dimensional Array:
Example:
2. Multi-Dimensional Array:
o Arrays that contain other arrays. The most common is the two-dimensional
array, also known as a matrix.
o Syntax: dataType[][] arrayName = new dataType[rows][columns];
Example: