Java Lab Internal1 Q&A
Java Lab Internal1 Q&A
Answer:
class Test
int a, b;
Test()
a=10;
b=20;
System.out.println("Value of a: "+a);
System.out.println("Value of b: "+b);
class TestDemo
Answer:
class Test
int a, b;
{
a=n1;
b=n2;
System.out.println("Value of a = "+a);
System.out.println("Value of b = "+b);
class TestDemo
Answer:
class Employee
int id;
String name;
this.id = id;
this.name = name;
void show()
System.out.println(id+" "+name);
}
class ThisDemo2
e1.show();
ANSWER:
class Addition
System.out.println(a+b);
System.out.println(a+b+c);
System.out.println(a+b);
class Methodload
obj.sum(10, 20);
obj.sum(10.05f, 15.20f);
ANSWER:
Person()
System.out.println("Introduction:");
Person(String name)
}
}
ANSWER:
class Animal
void eat()
System.out.println("animal is eating...");
void eat()
System.out.println("dog is eating...");
a.eat();
Answer:
if (n == 1)
return 1;
else
return(n * factorial(n-1));
Answer:
class TestMemberOuter
class Inner
void msg()
System.out.println("data is "+data);
in.msg();
}
9.Write a java program on super keyword at method level?
Answer:
class Animal
{ void eat()
System.out.println("eating..."); }
{ void eat()
{ eat(); super.eat(); }
}class TestSuper2
ANSWER:
class Bike
System.out.println("running");
void run()
{
honda.run();
ANSWER:
{ void draw()
System.out.println("drawing rectangle");
{ void draw()
System.out.println("drawing circle");
} } class TestAbstraction1
ANSWER:
interface Printable
System.out.println("Hello");
System.out.println("Welcome");
}}
class ExceptionDemo
try
int c=a/b;
catch (ArithmeticException e)
}
14.Write a java program on Simple Calculator?
ANSWER:
import java.util.Scanner;
System.out.print("Enter the type of operation you want to perform (+, -, *, /, %): ");
int result = 0;
if (operation.equals("+")) {
else if (operation.equals("-")) {
else if (operation.equals("*")) {
else if (operation.equals("%")) {
else if (operation.equals("/")) {
else {
System.out.println("Invalid operation");
return result;
ANSWER:
class Node{
int data;
Node previous;
Node next;
this.data = data;
if(head == null) {
head.previous = null;
//tail's next will point to null, as it is the last node of the list
tail.next = null;
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
newNode.previous = tail;
tail = newNode;
tail.next = null;
if(head == null) {
System.out.println("List is empty");
return;
}
while(current != null) {
current = current.next;
dList.addNode(1);
dList.addNode(2);
dList.addNode(3);
dList.addNode(4);
dList.addNode(5);
dList.display();
//First.java
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("welcome",150,150);
}
}
myapplet.html
<html>
<body>
</applet>
</body>
</html>
Or
//First.java
import java.applet.Applet;
import java.awt.Graphics;
g.drawString("welcome to applet",150,150);
/*
</applet>
*/
17.Write a java Java program To delete a node at a specific position in doubly linked list?
ANSWER:
int data;
Node prev;
Node next;
Node(int d) {
data = d;
prev = null;
next = null;
class GfG {
if (head == null) {
return head;
curr = curr.next;
if (curr == null) {
return head;
if (curr.prev != null) {
curr.prev.next = curr.next;
if (curr.next != null) {
curr.next.prev = curr.prev;
if (head == curr) {
head = curr.next;
return head;
curr = curr.next;
System.out.println();
// 1 <-> 2 <-> 3
head.next.prev = head;
head.next.next.prev = head.next;
head = delPos(head, 2);
printList(head);
18. Java Program to print the sum of all the items of the array?
Answer:
//Initialize array
int sum = 0;
Output:
Answer:
class SPalin {
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
else {
Answer:
package com.example;
return a + b;
return a - b;
return a * b;
if (b != 0) {
return a / b;
} else {
PackageExample.java
import com.example.Calculator;
Output:
Addition: 8
Subtraction: 2
Multiplication: 15
Division: 5