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

Java Skill Questions 13

The document presents a series of Java skill-based practical questions and answers that cover various programming concepts such as unit conversion, date-time calculations, and exception handling. Each example demonstrates specific skills gained, including object-oriented programming, collections, and multithreading. The document serves as a guide for learners to enhance their Java programming abilities through hands-on coding exercises.
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)
6 views5 pages

Java Skill Questions 13

The document presents a series of Java skill-based practical questions and answers that cover various programming concepts such as unit conversion, date-time calculations, and exception handling. Each example demonstrates specific skills gained, including object-oriented programming, collections, and multithreading. The document serves as a guide for learners to enhance their Java programming abilities through hands-on coding exercises.
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

Java Skill-Based Practical Questions & Answers

1. Design a Java-based Unit Conversion Utility (temperature, currency, length)


Type: Skill | Skills Gained: Type conversion, control structures, method declaration
public class UnitConverter {
public static double celsiusToFahrenheit(double c) {
return (c * 9/5) + 32; // formula
}
public static void main(String[] args) {
double c = 30.0;
double f = celsiusToFahrenheit(c);
System.out.println(c + "degC = " + f + "degF");
}
}

2. Build a Date-Time Calculator


Type: Skill | Skills Gained: java.time API (LocalDate, Period, Duration)
import java.time.*;

public class DateTimeCalculator {


public static void main(String[] args) {
LocalDate today = LocalDate.now();
LocalDate afterTen = today.plusDays(10); // add 10 days
Period diff = Period.between(today, afterTen); // difference
System.out.println("Today: " + today);
System.out.println("10 days later: " + afterTen);
System.out.println("Difference: " + diff.getDays() + " days");
}
}

3. Develop a Student Marks Analyzer


Type: Skill | Skills Gained: 2D arrays, enhanced for-loop
public class MarksAnalyzer {
public static void main(String[] args) {
int[][] marks = {
{90, 80, 70},
{75, 85, 65}
};
for (int i = 0; i < marks.length; i++) {
int total = 0;
for (int m : marks[i]) {
total += m;
}
System.out.println("Student " + (i+1) + " Average: " + (total / marks[i].length));
}
}
}

4. Implement a Vehicle Management System using Inheritance


Type: Skill | Skills Gained: Class design, inheritance, overriding, constructor chaining
class Vehicle {
String brand;
Vehicle(String brand) { this.brand = brand; }
void display() { System.out.println("Brand: " + brand); }
}

class Car extends Vehicle {


String model;
Car(String brand, String model) {
Java Skill-Based Practical Questions & Answers

super(brand); // call parent constructor


this.model = model;
}
@Override
void display() {
super.display();
System.out.println("Model: " + model);
}
public static void main(String[] args) {
new Car("Toyota", "Innova").display();
}
}

5. Develop a Role-Based Access Control (RBAC) System


Type: Skill | Skills Gained: Abstract classes, interfaces, dynamic binding
interface Role {
void permissions();
}

class Admin implements Role {


public void permissions() { System.out.println("Admin - full access"); }
}

class User implements Role {


public void permissions() { System.out.println("User - read-only access"); }
}

public class RBACDemo {


public static void main(String[] args) {
Role r1 = new Admin();
Role r2 = new User();
r1.permissions();
r2.permissions();
}
}

6. Create a Modular Library System with Package & Access Control


Type: Skill | Skills Gained: Access modifiers, modular design, encapsulation
class Book {
private String title; // private encapsulated field
public Book(String title) { this.title = title; }
public void info() { System.out.println("Book: " + title); }
}

public class Library {


public static void main(String[] args) {
Book b = new Book("Java Basics");
b.info();
}
}

7. Build a Custom Exception-Driven Banking Application


Type: Skill | Skills Gained: Custom exceptions, try-catch-finally, throw/throws
class LowBalanceException extends Exception {
LowBalanceException(String msg) { super(msg); }
}

class Bank {
Java Skill-Based Practical Questions & Answers

void withdraw(int balance, int amount) throws LowBalanceException {


if (amount > balance)
throw new LowBalanceException("Insufficient Funds");
System.out.println("Withdrawal successful!");
}
}

public class BankApp {


public static void main(String[] args) {
Bank bank = new Bank();
try {
bank.withdraw(1000, 1500);
} catch (LowBalanceException e) {
System.out.println("Error: " + e.getMessage());
} finally {
System.out.println("Thank you for banking with us!");
}
}
}

8. Develop a Mini Shopping Cart using Collections and Generics


Type: Skill | Skills Gained: ArrayList, HashMap, custom class with generics
import java.util.*;

public class ShoppingCart {


public static void main(String[] args) {
Map<String, Integer> cart = new HashMap<>();
cart.put("Shoes", 2);
cart.put("Shirt", 1);
cart.forEach((item, qty) -> System.out.println(item + " Qty: " + qty));
}
}

9. Implement a Product Sorting Engine


Type: Skill | Skills Gained: Comparable, Comparator, lambda expressions
import java.util.*;

class Product implements Comparable<Product> {


String name; int price;
Product(String name, int price) { this.name = name; this.price = price; }
public int compareTo(Product p) { return this.price - p.price; } // natural order
public String toString() { return name + ": " + price; }
}

public class SortProducts {


public static void main(String[] args) {
List<Product> list = Arrays.asList(
new Product("Pen", 10),
new Product("Book", 40),
new Product("Pencil", 5)
);
list.sort((a,b) -> a.price - b.price); // lambda comparator
System.out.println("Sorted by price: " + list);
}
}

10. Create a Functional Data Processing Pipeline


Type: Skill | Skills Gained: Stream API filter, map, reduce
Java Skill-Based Practical Questions & Answers

import java.util.*;
import java.util.stream.*;

public class DataPipeline {


public static void main(String[] args) {
List<Integer> nums = Arrays.asList(1,2,3,4,5,6);
int sumSquaresEven = nums.stream()
.filter(n -> n % 2 == 0)
.map(n -> n * n)
.reduce(0, Integer::sum);
System.out.println("Sum of squares of even numbers: " + sumSquaresEven);
}
}

11. Simulate a Multithreaded Ticket Booking System


Type: Skill | Skills Gained: Thread lifecycle, synchronization, race condition avoidance
class TicketBooking extends Thread {
static int tickets = 10;
public void run() {
synchronized (TicketBooking.class) { // class-level lock
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + " booked ticket " + tickets--);
} else {
System.out.println("Sold out");
}
}
}
public static void main(String[] args) {
for (int i = 0; i < 12; i++) new TicketBooking().start();
}
}

12. Build a Producer-Consumer Simulation with BlockingQueue


Type: Skill | Skills Gained: BlockingQueue, inter-thread communication, concurrency control
import java.util.concurrent.*;

public class ProducerConsumer {


public static void main(String[] args) {
BlockingQueue<Integer> q = new ArrayBlockingQueue<>(5);

Runnable producer = () -> {


try {
for (int i = 1; i <= 5; i++) {
q.put(i);
System.out.println("Produced: " + i);
}
} catch (InterruptedException e) {}
};

Runnable consumer = () -> {


try {
for (int i = 1; i <= 5; i++) {
System.out.println("Consumed: " + q.take());
}
} catch (InterruptedException e) {}
};

new Thread(producer).start();
new Thread(consumer).start();
}
Java Skill-Based Practical Questions & Answers

13. Design and Build a File-Based Contact Book with Serialization


Type: Skill | Skills Gained: File I/O, byte character streams, object serialization
import java.io.*;

class Contact implements Serializable {


String name; String phone;
Contact(String name, String phone) { this.name = name; this.phone = phone; }
}

public class ContactBook {


public static void main(String[] args) throws Exception {
Contact c = new Contact("Mammu", "9999999999");
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("contacts.ser"))) {
out.writeObject(c);
}
Contact read;
try(ObjectInputStream in = new ObjectInputStream(new FileInputStream("contacts.ser"))) {
read = (Contact) in.readObject();
}
System.out.println("Name: " + read.name + ", Phone: " + read.phone);
}
}

You might also like