1. Write a java program to access N number of for (int i = 0; i < word.
length(); i++) {
cities from users and display them in ascending char ch = [Link](i);
and descending order. // Check if the character is a letter and NOT a
import [Link]; vowel
import [Link]; if ([Link](ch) && ch != 'a' && ch
import [Link]; != 'e' && ch != 'i' && ch != 'o' && ch != 'u') {
public class CitySorter { nonVowelCount++;
public static void main(String[] args) { }
Scanner scanner = new Scanner([Link]); }
[Link]("Enter the number o [Link]("The number of non-vowel
f cities (N): "); letters in \"" + word + "\" is: " + nonVowelCount);
int n = [Link](); [Link]();
[Link](); // Consume the newline }
character }
String[] cities = new String[n];
// Accepting city names from the user 5. Explain [Link] package and [Link]
[Link]("Enter the names of " + n + package.
" cities:"); [Link] Package: This is the most
for (int i = 0; i < n; i++) { fundamental package in Java. It is automatically
cities[i] = [Link](); imported into every Java program without the
} need for an explicit import statement. It contains
// Displaying cities in Ascending Order (A-Z) core classes and interfaces that are essential for
[Link](cities); the language, such as:
[Link]("\nCities in Ascending o Object (the root class of all Java classes),
Order:"); o Wrapper classes (Integer, Double, Boolean),
for (String city : cities) { o String classes
[Link](city); (String, StringBuilder, StringBuffer),
} o Basic exception classes
// Displaying cities in Descending Order (Z-A) (Exception, Error, RuntimeException),
[Link](cities, [Link]()); o Math class (Math),
[Link]("\nCities in Descending o System class for standard I/O operations.
Order:");
for (String city : cities) { [Link] Package: This is the utility
[Link](city); package and is one of the most widely used
} packages. It contains a collection of utility
[Link](); classes and interfaces that provide powerful data
} structures and helpful functions. It must be
} explicitly imported. Key components include:
o Collection Framework: Interfaces
like Collection, List, Set, Map and their
2. Write a java program that displays the number implementations
of non-vowels in the given words. (ArrayList, LinkedList, HashSet, HashMap).
import [Link]; o Date and Time Classes: Legacy classes
public class NonVowelCounter { like Date, Calendar, and modern API introduced
public static void main(String[] args) { in Java 8 (LocalDate, LocalTime).
Scanner scanner = new Scanner([Link]); o Utility Classes: Scanner (for input), Arrays (for
[Link]("Enter a word: "); array manipulation), Collections (for collection
String word = manipulation), Random (for generating random
[Link]().toLowerCase(); // Convert to numbers).
lower case for easy comparison
int nonVowelCount = 0;
3. Write a java program store/accept n number Scanner ([Link]): A simple text
through command line and store all prime scanner used to parse primitive types and strings
numbers into an array and display elements of from input streams (like [Link]), files, or
array. strings. It uses delimiters (default is whitespace)
public class PrimeFromCLI { to break input into tokens.
public static void main(String[] args) { o Example: Scanner sc = new
// Check if command line arguments are Scanner([Link]); int x = [Link]();
provided Math ([Link]): A utility class
if ([Link] == 0) { providing static methods for common
[Link]("Please provide numbers mathematical operations
as command line arguments."); (e.g., sqrt(), pow(), sin(), cos()), as well as
return; constants like PI and E. Its methods can be
} called directly using the class name
int[] numbers = new int[[Link]]; ([Link](25)).
int[] primeArray = new int[[Link]]; // Max Date ([Link]): A legacy class that
possible size represents a specific instant in time, with
int primeCount = 0; millisecond precision. While still used, it's
largely superseded by the more
// Parse command line arguments to integers modern [Link] package
for (int i = 0; i < [Link]; i++) { (e.g., LocalDate, ZonedDateTime) introduced in
numbers[i] = [Link](args[i]); Java 8 for better design and functionality.
} Class ([Link]): Instances of
// Find prime numbers the Class class represent classes and interfaces in
for (int num : numbers) { a running Java application. It is the entry point
if (isPrime(num)) { for all of the Reflection API, which allows a
primeArray[primeCount] = num; program to inspect and manipulate itself. You
primeCount++; can get a Class object
} using [Link](), .class syntax,
} or [Link]().
// Display the prime numbers array 7. What is data type and list primitive and non-
[Link]("Prime numbers from the primitive data type.
given list:"); A data type defines the type of value a variable can
for (int i = 0; i < primeCount; i++) { hold, the operations that can be performed on it, and
[Link](primeArray[i] + " "); the amount of memory it occupies.
} Primitive Data Types: These are the basic
} building blocks of the Java language. They are
// Method to check if a number is prime predefined by the language and named by a
private static boolean isPrime(int num) { reserved keyword. They hold a single value of
if (num <= 1) { their type.
return false; o Numeric:
} byte, short, int, long (for integers)
for (int i = 2; i <= [Link](num); i++) { float, double (for floating-point numbers)
if (num % i == 0) { o Non-Numeric:
return false; char (for single characters)
} boolean (for true/false values)
} Non-Primitive Data Types (Reference
return true; Types): These are created by the programmer.
} They do not store the value directly; instead,
} they store the memory address (reference) of
To Run: java PrimeFromCLI 7 12 19 23 45 53 the object. The size is not fixed.
6. Explain Scanner, Math, Date, Class in Java. o String *) Arrays
o Classes (e.g., Scanner, ArrayList) oExample: void myMethod() { int localVar =
4. What is Java? Explain at least 5 features in 10; }
Java. 2. Instance Variable (Non-Static Field): A
Java is a high-level, robust, object-oriented, and variable declared inside a class but outside any
platform-independent programming language method. It is bound to an object (instance) of
developed by Sun Microsystems (now owned by the class. Each object has its own copy.
Oracle). It follows the principle of "Write Once, Run o Example: class Student { String name; //
Anywhere" (WORA), meaning compiled Java code instance variable }
can run on all platforms that support Java without 3. Static Variable (Class Variable): A variable
the need for recompilation. declared with the static keyword. It is
Five Key Features of Java: associated with the class itself, not with any
1. Platform Independent: Java code is compiled object. Only one copy exists and is shared
into bytecode, which is executed by the Java among all instances of the class.
Virtual Machine (JVM). This allows Java o Example: class Student { static String
programs to run on any device equipped with a schoolName = "ABC School"; }
JVM, making it highly portable.
2. Object-Oriented: Java is built around the
concepts of objects and classes. It supports 9. What is method of overloading and overriding
fundamental OOP principles like encapsulation, with example.
inheritance, polymorphism, and abstraction, Method Overloading (Compile-time
which help in creating modular and reusable Polymorphism): This occurs when multiple
code. methods in the same class have the same
3. Simple and Familiar: Java's syntax is similar to name but different parameters (different type,
C and C++, but it eliminates complex and error- number, or order of parameters). Return type
prone features like explicit pointers, operator alone is not sufficient to overload a method.
overloading, and multiple inheritance, making it class MathOps {
easier to learn and use. // Overloaded add methods
4. Secure: Java provides a secure execution int add(int a, int b) { return a + b; }
environment. Features like the bytecode verifier, double add(double a, double b) { return a + b; } //
lack of explicit pointers, and a security manager Different parameter type
that defines access rules for classes make it int add(int a, int b, int c) { return a + b + c; } //
suitable for developing tamper-free and virus- Different number of parameters
free systems. }
5. Multithreading: Java has built-in support for
multithreaded programming, allowing Method Overriding (Run-time
concurrent execution of two or more parts of a Polymorphism): This occurs when
program to maximize CPU utilization and a subclass provides a specific implementation
performance. for a method that is already defined in
its superclass. The method must have the same
name, return type, and parameters.
8. What is variable and types of variable with class Animal {
example. void makeSound() { [Link]("Animal
A variable is a named memory location used to makes a sound"); }
store data values that can be changed during }
program execution. class Dog extends Animal {
Types of Variables in Java: @Override
1. Local Variable: A variable declared inside a void makeSound() { [Link]("Dog
method, constructor, or block. It must be barks"); } // Overridden method
initialized before use and is only accessible }
within its scope.
o Declaration: int[] arr; or int arr[];
o Instantiation: arr = new int[5]; // Array of 5
11. Short note on class and object with an integers
example. 2. Multi-Dimensional Array: An array of
Class: A class is a blueprint or arrays. The most common is the two-
a template from which individual objects are dimensional array, which can be thought of as
created. It defines the properties (attributes as a table with rows and columns.
variables) and behaviors (actions as methods) o Declaration: int[][] matrix;
that the objects created from it will have. It is a o Instantiation: matrix = new int[3][3]; // A
logical entity. 3x3 matrix
Object: An object is a real-world entity that o Initialization: int[][] matrix = { {1, 2, 3}, {4,
has state and behavior. It is an instance of a 5, 6} };
class. When a class is defined, no memory is 10. Difference Between Constructor and Method
allocated until an object of that class is created. 1. **Purpose**
Example: Constructor:** Used to **initialize the state** of a
// Class (Blueprint) newly created object. Its primary role is to assign
class Car { initial values to the object's attributes.
// Attributes (State) Method:** Used to **expose the behavior** of an
String color; object. It defines the operations or actions that the
String model; object can perform.
2. Invocation
// Method (Behavior) Constructor: Invoked implicitly by the Java Virtual
void drive() { Machine (JVM) when the `new` keyword is used to
[Link](model + " is driving."); instantiate an object.
} Method: Invoked explicitly by the programmer
} using an object reference and the dot (`.`) operator
public class Main { (e.g., `[Link]()`).
public static void main(String[] args) { 3. Return Type
// Creating Objects (Instances of the Car class) Constructor: Has no return type, not even `void`.
Car car1 = new Car(); Method: Must have a return type. If it does not
[Link] = "Red"; return a value, the return type must be declared as
[Link] = "Toyota"; `void`.
[Link](); // Output: Toyota is driving. 4. Name
Constructor: Must have the exact same name as the
Car car2 = new Car(); class it belongs to (case-sensitive).
[Link] = "Blue"; Method: Can have any valid identifier name (usually
[Link] = "Honda"; a verb) and follows standard naming conventions
[Link](); // Output: Honda is driving. like camelCase.
} 5. Inheritance
} Constructor: Cannot be inherited by a subclass. A
subclass constructor can, however, call a superclass
12. What is array explain its types. constructor using the `super()` keyword.
An array is a container object that holds a fixed Method: Can be inherited by a subclass and can also
number of values of a single type. The length of an be overridden to provide a specific implementation.
array is established when the array is created and 6. Default Availability
cannot be changed. Each item in an array is called Constructor: If no constructor is defined in a class,
an element, and each element is accessed by its the Java compiler automatically provides a default
numerical index. no-argument constructor.
Types of Arrays: Method: The compiler does not provide any default
1. Single-Dimensional Array: A linear array that methods. All methods must be explicitly defined by
stores elements in a single row. the programmer.
0.0