Java Assignment- 2
Arrays, String Handling, Packages & Modules
Basic Questions
- Write a program that creates an int[] with values {3, 6, 9, 12, 15} and prints all elements using a for loop on one line separated by spaces.
- Write a program that reads 5 integers from the user using Scanner, stores them in an array, and prints their sum and average.
- Write a program that copies all elements from one int[] to another int[] of the same length and prints the copied array.
- Write a program that finds and prints the largest and smallest values in an int[] {10, -3, 25, 7, 0}.
- Write a program that counts how many even numbers are present in an int[] {2, 5, 8, 11, 14, 17} and prints the count.
- Write a program that creates a 2 x 3 matrix using int[][], fills it with { {1,2,3}, {4,5,6} }, and prints it row by row.
- Write a program that declares String name = “Java Programming” and prints the length, the first character, and the last character.
- Write a program that compares two strings s1 = “Hello” and s2 = “hello” and prints results of case-sensitive equality and case-insensitive equality.
- Write a program that joins firstName = “Ada” and lastName = “Lovelace” with a space between them using + and prints the full name.
- Write a program that checks if the string “Learn Java Today” contains “Java” using contains() and prints the index using indexOf().
- Write a program that replaces “Java” with “Kotlin” in the string “I love Java” and prints the new string.
- Write a program that trims leading and trailing spaces from the string ” data ” and prints the original and trimmed lengths.
- Write a program that splits “red,green,blue” by comma into a String[] and prints each color on a new line.
- Write a program that uses StringBuilder to append three words “fast”, ” and “, and “safe” and then prints the final string.
- Write a program that creates both a StringBuilder and a StringBuffer, appends “ABC” and “123” to each, and prints the two final strings.
- Write a program that creates int[] a = new int[3] and prints all elements to show their default values.
- Write a class with a static variable count and a non-static variable name. In main, create two objects, set their names, increase count in the constructor, and print both names and the final count.
- Write a class with a static method square(int x) and a non-static method cube(int x). In main, call both methods correctly and print their results.
- Write a program that uses Scanner to read an integer age and then a full line for a user’s name. Print both values. Ensure the newline after the integer is handled correctly.
- Write a program that converts a primitive int x = 42 to an Integer (autoboxing), converts it back to int (unboxing), and prints both values.
Intermediate Questions
- Write a program that reverses an int[] in place (without creating a second array) and prints the array before and after reversing.
- Write a program that merges two arrays {1,3,5} and {2,4,6} into a third array and prints the merged result (order can be simple concatenation).
- Write a program that performs a linear search for a user-given number in an int[] and prints the index if found or a “not found” message.
- Write a program that sorts an int[] in ascending order using Arrays.sort() and prints the sorted array.
- Write a program that computes the transpose of a 2 x 3 matrix into a 3 x 2 matrix and prints both matrices.
- Write a program that computes and prints the sum of each row and the sum of each column of a 2D integer array.
- Write a program that finds and prints the second largest number in an int[] without sorting the array.
- Write a program that removes duplicate values from an int[] and prints the array of unique values in the original order (use simple nested loops, no Set).
- Write a program that counts vowels, consonants, digits, and spaces in a user-entered line of text and prints the counts.
- Write a program that reverses a string without using StringBuilder.reverse() (use a character array or manual swapping) and prints the result.
- Write a program that checks if a user-entered string is a palindrome, ignoring case and spaces, and prints a clear message.
- Write a program that counts the frequency of each character in a string and prints the counts (consider only basic ASCII for simplicity).
- Write a program that builds a large string by appending numbers from 1 to 5000 using String with + in a loop, measures the time with System.nanoTime(), and prints the elapsed time.
- Write a program that repeats question 33 using StringBuilder (append in a loop), measures the time, and prints the elapsed time so you can compare with question 33.
- Write a program that passes a String to a method which tries to change it by concatenation. Print the original string before and after the method call to show that String is immutable.
- Write a program that passes a StringBuilder to a method which appends extra text to it. Print the builder’s content before and after the method call to show that it is mutable.
- Write a short program (with comments) that creates a primitive int and an object Integer, explains in comments which goes on the stack and which object is on the heap, and prints their values.
- Create a package util.text with a class TextUtils that has a static method countWords(String). Create another package app with Main that imports and uses TextUtils. Organize folders to match packages and run using javac -cp . and java -cp . app.Main.
- Write a program that uses a fully qualified class name to call java.util.Arrays.toString() without an import statement and explain in comments why this works.
- Write a console application that asks the user for name, age, and three marks, then prints a clean summary report with the average. Use Scanner and handle input carefully.
Advanced Questions
- Write a program that multiplies two matrices A and B (dimensions compatible) using 3 nested loops and prints the result matrix.
- Write a program that performs binary search on a sorted int[]. Read a target from the user, search it, and print the index or a “not found” message.
- Write a program that reads an unknown number of positive integers from the user until -1 is entered. Use a growing array strategy with Arrays.copyOf() to store all inputs, then print the final array and its length.
- Write a method that rotates an int[] to the right by k positions. Handle cases where k is larger than the array length. Show input and rotated output.
- Write a method that converts a sentence to “title case” where each word starts with an uppercase letter and the rest are lowercase. Handle multiple spaces gracefully.
- Write a program that removes all duplicate characters from a string while keeping the first occurrence of each character. Print the original and the result (e.g., “programming” → “progamin”).
- Write a program that compares the time to build a long string (e.g., appending numbers from 1 to 50,000) using StringBuilder and using StringBuffer. Measure time for both using System.nanoTime() and print which one was faster on your system.
- Create a simple modular project with two modules: com.util (exports package com.util) and com.app (requires com.util). Put one public class in each module and a module-info.java for both. Compile with
- Create a small utility in package util named StringUtils and build it into a JAR util.jar. Write a main class in package app that uses util.StringUtils. Show the exact javac commands to compile, the jar command to build util.jar, and the java -cp .;util.jar app.Main (or : on Linux/Mac) command to run.
- Write a program that sets Integer n = null; then tries int x = n; inside a try block, catches NullPointerException, and prints a friendly message explaining what happened (autounboxing with null).
javac --module-source-path src -d out $(find src -name "*.java") and run with java --module-path out --module com.app/com.app.Main.
Write the code files and the exact commands as part of your answer.