Basis Java
Basis Java
Answer:
import java.util.Scanner;
public class Solution2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms for the Fibonacci series: ");
int terms = scanner.nextInt();
if (terms <= 0) {
System.out.println("Please enter a positive integer.");
} else {
System.out.println("Fibonacci series up to " + terms + " terms:");
int first = 0, second = 1;
for (int i = 1; i <= terms; i++) {
System.out.print(first + " ");
int next = first + second;
first = second;
second = next;
}
}
}
}
Output:
Enter the number of terms for the Fibonacci series: 5
Fibonacci series up to 5 terms: 0 1 1 2 3
3. Write a java program to find the roots of a quadratic equation.
Answer:
Output:
Answer:
Output:
100
100.0°C is equal to 212°F
4. Using java command line argument finds the average of any given set of numbers.
Answer:
Output:
java Q4 2 5 7 16 20
Average: 10.0
Answer:
import java.util.Arrays;
Output:
Answer:
import java.util.Scanner;
}
}
Output:
Dog barks.
Cat meows.
7. Write a program to find the even sum and the odd sum in a given array. Write a program
demonstrate method overloading.
Answer:
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
Calculator calc = new Calculator();
System.out.println("Sum (int, int): " + calc.add(5, 10));
System.out.println("Sum (double, double): " + calc.add(2.5, 3.5));
System.out.println("Sum (int, int, int): " + calc.add(1, 2, 3));
}
}
Output:
Even Sum: 20
Odd Sum: 16
Answer:
import java.io.File;
public class FileInfo {
public static void main(String[] args) {
File file = new File("example.txt");
System.out.println("Path: " + file.getAbsolutePath());
System.out.println("Exists: " + file.exists());
if (file.exists()) {
System.out.println("Size: " + file.length() + " bytes");
}
}
}
Output:
Exists: true
Size: 0 bytes
Answer:
Import java.util.*;
Output:
Result of the series: 0.33333333333333337
10. Write a java program to create an abstract class named Shape that contains two integers
and an empty method named printArea(). Provide three classes named Rectangle,
Triangle and Circle such that each one of the classes extends the class Shape. Each
one of the classes contain only the method printArea( ) that prints the area of the
given shape.
Answer:
import java.util.Scanner;
void printArea() {
int area = dimension1 * dimension2;
System.out.println("Area of Rectangle: " + area);
}
}
void printArea() {
double area = 0.5 * dimension1 * dimension2;
System.out.println("Area of Triangle: " + area);
}
}
void printArea() {
double area = Math.PI * dimension1 * dimension1;
System.out.println("Area of Circle: " + area);
}
}
scanner.close();
}
}
Output:
Answer:
import java.util.Scanner;
class Number {
private int[] numbers;
public void read(int n) {
Scanner sc = new Scanner(System.in);
numbers = new int[n];
System.out.println("Enter " + n + " integers:");
for (int i = 0; i < n; i++) {
numbers[i] = sc.nextInt();
}
}
public void swap() {
if (numbers == null || numbers.length < 2) return;
int maxIndex = 0, minIndex = 0;
for (int i = 1; i < numbers.length; i++) {
if (numbers[i] > numbers[maxIndex]) maxIndex = i;
Output:
Enter 5 integers: 5 10 2 50 3
Array after swapping:
5 10 50 2 3
12. Write a program in java using inheritance to show how to call the base class
parameterized constructors from the derived class using super.
Answer:
class Base {
Base(int a) {
System.out.println("Base class constructor called with value: " + a);
}
}
class Derived extends Base {
Derived(int a, int b) {
super(a);
System.out.println("Derived class constructor called with value: " + b);
}
}
public class InheritanceExample {
public static void main(String[] args) {
Derived obj = new Derived(10, 20);
}
}
Output:
Output:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Even Sum: 6
Odd Sum: 9
14. Write a program to demonstrate method overriding. Write a program to concatenate two
strings.
A.Answer:
class Parent {
void display() {
System.out.println("This is the parent class method.");
}
}
class Child extends Parent {
void display() {
System.out.println("This is the child class method.");
}
}
public class MethodOverridingExample {
public static void main(String[] args) {
Parent obj = new Child();
obj.display();
}
}
Output:
B.Answer:
Output:
Answer:
interface A {
void methodA();
}
interface B {
void methodB();}
class C implements A, B {
public void methodA() {
System.out.println("Method A from interface A");
}
public void methodB() {
System.out.println("Method B from interface B");
}
}
class SumCalculator {
int sum(int a, int b) {
return a + b;
}
double sum(double a, double b) {
return a + b;
}
int sum(int a, int b, int c) {
return a + b + c;
}
}
public class MultipleInheritanceExample {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
SumCalculator calc = new SumCalculator();
System.out.println("Sum (int, int): " + calc.sum(5, 10));
System.out.println("Sum (double, double): " + calc.sum(2.5, 3.5));
System.out.println("Sum (int, int, int): " + calc.sum(1, 2, 3));
}
}
Output:
Method A from interface A
Method B from interface B
Sum (int, int): 15
Sum (double, double): 6.0
Sum (int, int, int): 6
17,18: Write a program to demonstrate constructor overloading & default constructor. Write
a complete Java program to print the Fibonacci series up to n terms. Input should
be taken using Buffered Reader class, Exception must be handled with custom
error message.
A.Answer:
class SolutionA {
int value;
DemoConstructor() {
value = 0;
System.out.println("Default Constructor called. Value = " + value);
}
DemoConstructor(int value) {
this.value = value;
System.out.println("Overloaded Constructor called. Value = " + value);
}
}
public class ConstructorDemo {
public static void main(String[] args) {
DemoConstructor obj1 = new DemoConstructor();
DemoConstructor obj2 = new DemoConstructor(42);
}
}
Output:
Enter the number of terms:10
B.Answer:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
A.Answer
Output:
$ javac Solution19.java
$ java Solution19.java 5
>>> Factorial of 5 is 120
B.Answer
class Solution19B{
private String name;
private int age;
public ConstructorExample() {
name = "Unknown";
age = 0;
System.out.println("Default constructor called.");
}
Output:
A.Answer:
class ShallowCopy {
int[] arr;
ShallowCopy(int[] arr) {
this.arr = arr;
}
}
class DeepCopy {
int[] arr;
DeepCopy(int[] arr) {
this.arr = arr.clone();
}
}
Output:
Shallow Copy: 99
Deep Copy: 1
Answer:
Exception caught: Age is less than 30, not allowed
21. Write a program to demonstrate multi catch statement using command line argument.
One object in Java can be assigned as reference to another object of the same type.
To explain this concept write a complete Java program and explain how a reference
object works.
A) Answer:
Output:
$ javac Solution21A.java
$ java Solution21A.java
>>> Please provide two numbers as arguments.
$ java Solution21A.java ten 5
>>> Error: Please provide valid numbers.
$ java Solution21A.java 10 0
>>> Error: Division by zero is not allowed.
$ java Solution21A.java 10 2
>>> Result: 5
B) Answer:
class Demo{
int value;
Demo(int value) {
this.value = value;
}
}
public class ObjectReferenceExample {
public static void main(String[] args) {
Demo obj1 = newDemo(10);
Demo obj2 = obj1;
obj2.value = 20;
System.out.println("obj1.value: " + obj1.value);
System.out.println("obj2.value: " + obj2.value);
}
}
Output:
obj1.value: 20
obj2.value: 20
22. Write a program to demonstrate multi-level inheritance. Write a program to demonstrate
the method overloading for area() function.
A)Answer:
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Mammal extends Animal {
void walk() {
System.out.println("This mammal can walk.");
}
}
class Dog extends Mammal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Solution22A {
public static void main(String[] args) {
Output:
This animal eats food.
This mammal can walk.
The dog barks.
B)Answer:
class Area{
double area(double side) {
return side * side;
}
Output:
Answer:
class Shape {
void area() {
System.out.println("Area of shape is undefined.");
}
}
Circle(double radius) {
this.radius = radius;
}
void area() {
System.out.println("Area of circle: " + (Math.PI * radius * radius));
}
}
void area() {
System.out.println("Area of rectangle: " + (length * breadth));
}
}
Answer 1:
class Example {
void display() {
System.out.println("Object created without 'new' keyword.");
}
}
OUTPUT:
Object created without 'new' keyword.
Answer 2:
public class MatrixMultiplication {
public static void main(String[] args) {
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6}
};
int[][] matrix2 = {
{7, 8},
{9, 10},
{11, 12}
};
OUTPUT:
Resultant matrix:
58 64
139 154
25. Write a program to find whether a given string is a palindrome or not using command
line argument.
Answer:
OUTPUT:
$ javac Solution25.java
$ java Solution25.java madam
>>> The string is a palindrome
$ java Solution25.java Helo
>>> The string is not a palindrome
26.Write a Java program which creates a class named "Employee" having the following
members: Name, Age, Phone number, Address, Salary. It also has a method named
'printSalary()' which prints the salary of the Employee. Two classes 'Officer' and
'Manager' classes have data members 'specialisation' and 'department' respectively.
Now, assign name, age, phone number, address and salary to an officer and a
manager by making an object of both of these classes and print the same.
Answer:
class Employee {
private String name;
private int age;
private String phoneNumber;
private String address;
private double salary;
Employee(String name, int age, String phoneNumber, String address, double salary) {
this.name = name;
this.age = age;
this.phoneNumber = phoneNumber;
this.address = address;
this.salary = salary;
}
void printSalary() {
System.out.println("Salary of " + name + ": " + salary);
}
void show() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Phone Number: " + phoneNumber);
System.out.println("Address: " + address);
printSalary();
}
}
class Officer extends Employee {
String specialisation;
Officer(String name, int age, String phoneNumber, String address, double salary, String
specialisation) {
super(name, age, phoneNumber, address, salary);
this.specialisation = specialisation;
}
void show() {
super.show();
System.out.println("Specialisation: " + specialisation);
}
}
class Manager extends Employee {
String department;
Manager(String name, int age, String phoneNumber, String address, double salary, String
department) {
super(name, age, phoneNumber, address, salary);
this.department = department;
}
void show() {
super.show();
System.out.println("Department: " + department);
}
}
Output:
Officer Details:
Name: Alice
Age: 35
Phone Number: 9876543210
Address: 123 Street, City
Salary of Alice: 75000.0
Specialisation: IT
Manager Details:
Name: Bob
Age: 40
Phone Number: 8765432109
Address: 456 Avenue, City
Salary of Bob: 85000.0
Department: Sales