Core Java Practical Questions
Core Java Practical Questions
string.
System.out.print(input.charAt(i));
if (names[i].equals(target)) {
index = i;
break;
if (index != -1) {
} else {
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class AsciiValuesFromFile {
int c;
} catch (IOException e) {
e.printStackTrace();
Import javax.swing.*;
Import java.awt.event.ActionEvent;
Import java.awt.event.ActionListener;
Button.addActionListener(new ActionListener() {
List.setModel(model);
});
Frame.setLayout(new java.awt.BorderLayout());
Frame.add(button, java.awt.BorderLayout.NORTH);
Frame.setSize(300, 200);
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Frame.setVisible(true);
series:
i) Fibonacci series
Fibonacci.java
Package Series;
Int a = 0, b = 1;
System.out.print(a + “ “);
Int sum a + b;
A = b;
B = sum;
CubeOfNumbers.java
Package Series;
Int cube = I * I * I;
System.out.print(cube + “ “);
SquareOfNumbers.java
Package Series;
System.out.print(“Square of Numbers (“ + n +
“ terms): “);
Int square = I * I;
System.out.print(square + “ “);
SeriesMain.java
Import Series.*;
Int n = 10;
Fibonacci.generateFibonacciSeries(n); System.out.println();
CubeOfNumbers.generateCubeSeries(n);
System.out.println();
SquareOfNumbers.generateSquareSeries(n);
System.out.println();
Number /= 10;
}
Return sum == original;
If (isArmstrong(num)) {
} else {
Import java.io.BufferedReader;
Import java.io.BufferedWriter;
Import java.io.FileReader;
Import java.io.FileWriter;
Import java.io.IOException;
String line;
Writer.write(line);
Writer.newLine();
} catch (IOException e) {
e.printStackTrace();
If (Character.isDigit©) {
Return true;
Return false;
Import java.awt.*;
Import java.awt.event.*;
Button b1;
setLayout(new FlowLayout());
b1 = new Button(“Submit”);
add(l1);
add(tf1);
add(l2);
add(tf2);
add(l3);
add(tf3);
add(b1);
b1.addActionListener(this);
setSize(300, 200);
setVisible(true);
Sid = tf1.getText();
Sname = tf2.getText();
Saddr = tf3.getText();
This.setVisible(false);
displayFrame.add(sidLabel);
displayFrame.add(snameLabel);
displayFrame.add(saddrLabel);
displayFrame.setSize(300, 200);
displayFrame.setVisible(true);
displayFrame.addWindowListener(new WindowAdapter() {
System.exit(0);
});
New StudentDetailsAWT();
Package MCA;
String sid;
String sname;
Int marks1, marks2, marks3;
Public Student(String sid, String sname, int marks1, int marks2, int
marks3) {
This.sid = sid;
This.sname = sname;
This.marks1 = marks1;
This.marks2 = marks2;
This.marks3 = marks3;
System.out.println(“Marks 1: “ + marks1);
System.out.println(“Marks 2: “ + marks2);
System.out.println(“Marks 3: “ + marks3);
Student.display();
}
12)Write Java program which accepts string from user, if its length
is less than five, then throw user defined exception “Invalid String”
otherwise display string in uppercase.
import java.util.Scanner;
super(message);
if (str.length() < 5) {
} else {
try {
validateString(input);
} catch (InvalidStringException e) {
System.out.println(e.getMessage());
} finally {
scanner.close();
Import java.applet.*;
Import java.awt.*;
Import java.awt.event.*;
Public class LoginFormApplet extends Applet implements
ActionListener {
Label userLabel, passLabel, messageLabel;
TextField userField, passField;
Button loginButton;
Public void init() {
setLayout(new GridLayout(4, 2));
userLabel = new Label(“Username:”);
passLabel = new Label(“Password:”);
userField = new TextField(20);
passField = new TextField(20);
passField.setEchoChar(‘*’);
loginButton = new Button(“Login”);
messageLabel = new Label();
add(userLabel);
add(userField);
add(passLabel);
add(passField);
add(new Label());
add(loginButton);
add(messageLabel); loginButton.addActionListener(this);
}
Public void actionPerformed(ActionEvent ae) {
String username = userField.getText();
String password = passField.getText();
messageLabel.setText(“Login Successful”);
} else {
messageLabel.setText(“Invalid Username or Password”);
}
}
}
Import java.util.Scanner;
Public class Factorial {
Public static int factorial(int n) {
If (n == 0) { // Base case: factorial of 0 is 1
Return 1;
} else {
Return n * factorial(n – 1); // Recursive case
}
}
Public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(“Enter a number: “);
Int number = scanner.nextInt();
Scanner.close();
If (number < 0) {
System.out.println(“Factorial is not defined for negative
numbers.”);
} else {
Int result = factorial(number);
System.out.println(“Factorial of “ + number + “ is “ + result);
}
}
}
Import java.applet.Applet;
Import java.awt.Color;
Import java.awt.Graphics;
g.setColor(Color.BLACK);
g.setColor(Color.RED);
<!DOCTYPE html>
<html>
<head>
<title>Smiley Applet</title>
</head>
<body>
</applet>
</body>
</html>
16)Write a java program to copy the dates from one file into another
file.
Import java.io.FileReader;
Import java.io.FileWriter;
Import java.io.IOException;
Return;
Int ch;
Fw.write(ch);
} catch (IOException e) {
e.printStackTrace();
17)Write a java program to accept’ ‘n’ integers from the user &
store them
Import java.util.ArrayList;
Import java.util.Collections;
Import java.util.Scanner;
Int n = scanner.nextInt();
System.out.println(“Enter “ + n + “ integers:”);
Numbers.add(number);
Collections.reverse(numbers);
System.out.println(num);
Scanner.close();