0% found this document useful (0 votes)
3 views5 pages

Java JSP Solutions

Java practical questions

Uploaded by

km2zgxtfsc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Java JSP Solutions

Java practical questions

Uploaded by

km2zgxtfsc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

End Term Examination Solutions (Java & JSP)

1. Java Program: Animal Class with Subclass Cheetah

class Animal {

public void move() {

System.out.println("Animal is moving");

class Cheetah extends Animal {

@Override

public void move() {

System.out.println("Cheetah is running fast!");

public class Main {

public static void main(String[] args) {

Animal a = new Animal();

Cheetah c = new Cheetah();

a.move(); // Output: Animal is moving

c.move(); // Output: Cheetah is running fast!

}
2. Java Program: Search Element in Array List

import java.util.ArrayList;

import java.util.Scanner;

public class SearchElement {

public static void main(String[] args) {

ArrayList<Integer> list = new ArrayList<>();

list.add(10);

list.add(20);

list.add(30);

list.add(40);

list.add(50);

Scanner scanner = new Scanner(System.in);

System.out.println("Enter element to search: ");

int element = scanner.nextInt();

if (list.contains(element)) {

System.out.println("Element found!");

} else {

System.out.println("Element not found.");

scanner.close();

}
3. Java Program: Count Occurrences of Digit 5 in a File

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

public class CountDigit {

public static void main(String[] args) {

if (args.length != 1) {

System.out.println("Usage: java CountDigit <filename>");

return;

String fileName = args[0];

int count = 0;

try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {

String line;

while ((line = br.readLine()) != null) {

for (char ch : line.toCharArray()) {

if (ch == '5') {

count++;

} catch (IOException e) {

System.out.println("File not found or unable to read.");


}

System.out.println("Number of occurrences of digit 5: " + count);

4. JSP Program: Username and Password Check

<%@ page import="java.io.*, java.util.*" %>

<html>

<body>

<form method="post">

Username: <input type="text" name="username"><br>

Password: <input type="password" name="password"><br>

<input type="submit" value="Login">

</form>

<%

String correctUsername = "admin";

String correctPassword = "1234";

String username = request.getParameter("username");

String password = request.getParameter("password");

if (username != null && password != null) {

if (username.equals(correctUsername) && password.equals(correctPassword)) {

out.println("Welcome! <a href='index.jsp'>Proceed to Index Page</a>");


} else {

out.println("Incorrect username or password.");

%>

</body>

</html>

You might also like