2023 Winter
2023 Winter
Logical Operators:
&& (Logical AND)
|| (Logical OR)
Bitwise Operators:
& (Bitwise AND)
| (Bitwise OR)
b) Define constructor.
dataType[] arrayName;
java
Copy code
import packageName.ClassName;
or
import packageName.*;
java
Copy code
packageName.ClassName obj = new packageName.ClassName();
e) Differentiate between starting thread with run ( ) method and start ( ) method.
Q.2
import java.util.Arrays;
A thread is a lightweight sub-process and the smallest unit of a process. It enables concurrent
execution of tasks within a program.
Runnable: The thread is ready to run and waiting for CPU allocation
Blocked/Waiting: The thread is temporarily inactive and waiting for a resource or a signal.
import java.io.*;
import java.util.Scanner;
int wordCount = 0;
while (scanner.hasNext()) {
scanner.next();
wordCount++;
scanner.close();
Q.3
a) Write a program which displays functioning of ATM machine, (Hint : Withdraw, Deposit, Check
Balance and Exit)
import java.util.Scanner;
int choice;
do {
choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Insufficient balance!");
} else {
balance -= withdrawAmount;
break;
case 2:
balance += scanner.nextDouble();
break;
case 3:
break;
case 4:
break;
default:
System.out.println("Invalid choice!");
scanner.close();
}
b).Differentiate between method overloading and method overriding.
Q.4
Definition: Implicit type conversion, also known as widening or automatic conversion, occurs when
a lower data type is automatically converted into a higher data type without any explicit instructions
from the programmer.
Definition: Explicit type conversion, also known as narrowing or manual casting, occurs when a
higher data type is explicitly converted to a lower data type, which might result in a loss of data.
String name;
int age;
this.name = name;
this.age = age;
this.name = other.name;
this.age = other.age;
person1.display();
person2.display();
class Animal {
void eat() {
System.out.println("Animal is eating");
void bark() {
System.out.println("Dog is barking");
void meow() {
System.out.println("Cat is meowing");
dog.eat();
dog.bark();
cat.eat();
cat.meow();
Q.5
a) Explain vector with the help of example. Explain any 3 methods of vector class.
A Vector is a part of the Java Collection Framework that implements a growable array of objects. It
is similar to an array but can dynamically resize itself when elements are added or removed.
It can hold any type of objects and provides methods to manipulate and access the elements.
vector.add("Apple");
vector.add("Banana");
vector.add("Cherry");
b) Develop and Interest Interface which contains Simple Interest and Compound Interest methods
and static final field of rate 25%. Write a class to implement those methods.
interface Interest {
int time = 2;
System.out.println(calc.SimpleInterest(principal, time));
System.out.println(calc.CompoundInterest(principal, time));
c) Write a program that throws an exception called “NoMatchException” when a string is not equal
to “India”.
public NoMatchException() {
super(message);
if (!country.equals("India")) {
} else {
validateCountry("India");
validateCountry("USA");
} catch (NoMatchException e) {
Q.6
a) Write a program to print the sum, difference and product of two complex numbers by creating a
class named “Complex” with separate methods for each operation whose real and imaginary parts
are entered by user
import java.util.Scanner;
class Complex {
this.real = real;
this.imag = imag;
Complex add(Complex c) {
Complex subtract(Complex c) {
Complex multiply(Complex c) {
return new Complex(this.real * c.real - this.imag * c.imag, this.real * c.imag + this.imag * c.real);
}
@Override
return real + (imag >= 0 ? " + " : " - ") + Math.abs(imag) + "i";
sc.close();
b)
Errors in programming refer to issues or faults that prevent a program from functioning correctly.
They can occur at different stages of the program execution: compilation, runtime, or during logical
operations
1. Syntax Errors:
Definition: These errors occur when the rules of the programming language are violated.
2. Runtime Errors:
3. Logical Errors:
Definition: These errors occur when the program runs without crashing but produces
incorrect results.
In Java, threads can have priorities that influence the order in which threads are scheduled for
execution.
Thread priorities allow you to suggest to the thread scheduler which threads should get preference
for CPU time, but the final behavior depends on the operating system's thread scheduling
mechanism.
thread.setPriority(int priority);