H. T. No.
CHAITANYA BHARATHI INSTITUTE OF TECHNOLOGY (A)
Gandipet, Hyderabad -75
Department of Information Technology
B.E. / B. Tech (Common to CSE, IT, AI&DS, CET, CSE-AI&ML)
SUBJECT: JAVA PROGRAMMING (22ITC02N)
Class Test-I
Date: 8-9-2025 Time: 10.30 AM to 11.30AM
SEMESTER: III Key/Solutions Max. Marks: 20
Answer ALL questions. All parts of the questions must be answered at one place only.
SECTION – A 3 X 2 = 06M
1 Describe the following methods of the String class. CO1 BL2 [2M]
(i) concat (ii) intern
concat():
Used to concatenate (join) one string with another.
Example:
String s1 = "Hello";
String s2 = "World";
String s3 = s1.concat(" " + s2); // Hello World
(ii) intern():
Returns a canonical representation of the string from the
String pool.Ensures that all equal strings share the same
memory reference.
Example:
String s1 = new String("CBIT");
String s2 = s1.intern();
String s3 = "CBIT";
System.out.println(s2 == s3); // true
2 Does Java support multiple inheritance? Justify your answer. CO2 BL2 [2M]
Java does not support multiple inheritance with classes to
avoid ambiguity .
However, Java supports multiple inheritance via interfaces.
Example:
interface A { void show(); }
interface B { void display(); }
class C implements A, B {
public void show() { System.out.println("Show A"); }
public void display() { System.out.println("Display B"); }
}
3 What is the purpose of finally block in exception handling? CO3 BL1 [2M]
The finally block is always executed whether an exception
occurs or not.
It is generally used to release resources (files, DB
connections, sockets).
Example:
try {
int x = 10 / 0;
} catch(Exception e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block executed");
}
SECTION – B 2x7=14M
4 a Differentiate between the “static” and “final” keywords in CO1 BL2 [3M]
Java.
Feature static final
variables,
Applies
methods, blocks, variables, methods, classes
to
nested classes
Belongs to class,
Meanin Constant (cannot be changed/
shared by all
g overridden)
objects
Single copy for all
Variable Value cannot be reassigned
objects
Can be called
Method Cannot be overridden
without object
Class – Cannot be inherited
Distinguish between runtime polymorphism and compile
time polymorphism with suitable code examples. CO1 BL2 [4M]
Compile-time polymorphism (Method Overloading):
class Calculator
{
int add(int a, int b)
{ return a + b; }
double add(double a, double b)
{ return a + b; }
}
b
Runtime polymorphism (Method Overriding):
class Animal {
void sound()
{ System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() {
System.out.println("Bark");
}
}
OR
5 a Describe the key differences in how the protected access CO1 BL3 [3M ]
modifier behaves with respect to inheritance when the
subclass is in the same package versus a different package.
Provide a code example for both scenarios to illustrate the
access rules.
Same package: Subclass can access protected members
directly.
Different package: Subclass can access protected members
only through inheritance (not via object).
Example:
// Same package
class A {
protected int x = 10;
}
class B extends A {
void show() {
System.out.println(x);
}
}
// Different package
package p1;
public class A {
protected void msg() {
System.out.println("Hello");
}
}
package p2;
import p1.A;
class B extends A {
void display()
{
msg();
}
}
Differentiate between abstract classes and Interfaces with BL2
examples.
b CO2 [4M]
Points Abstract Class Interface
Cannot be instantiated;
contains both abstract Specifies a set of methods a class
(without implementation) and must implement; methods are
concrete methods (with abstract by default.
Definition implementation)
Methods are abstract by default;
Implementation Can have both implemented
Java 8, can have default and static
and abstract methods.
Method methods.
class can inherit from only A class can implement multiple
Inheritance one abstract class. interfaces.
Methods and properties can
Access Methods and properties are
have any access modifier
implicitly public.
Modifiers (public, protected, private).
Can have member variables
Variables are implicitly public,
(final, non-final, static, non-
static, and final (constants).
Variables static).
6 a What are the various ways of creating Inner class ? Write a CO2 BL1 [4M]
Java Program to demonstrate Anonymous inner class ?
Types of inner classes:
● Member inner class
● Static nested class
● Local inner class
● Anonymous inner class
Example (Anonymous inner class):
abstract class Greeting {
abstract void sayHello();
}
class Test {
public static void main(String[] args) {
Greeting g = new Greeting() {
void sayHello() { System.out.println("Hello from
Anonymous class"); }
};
g.sayHello();
}
}
b How to create and use a package in a Java program? CO2 BL2 [3M]
Create package:
package mypack;
public class A { public void msg() { System.out.println("Hello
Package"); } }
Compile: javac -d . A.java
Use in another program:
import mypack.A;
class Test {
public static void main(String[] args) {
A obj = new A();
obj.msg();
}
}
OR
7 a Explain how to handle multiple exceptions using multi catch CO3 BL2 [3M]
block with example?
public class ExcepTest {
public static void main(String args[]) {
try {
int a[] = new int[2];
int b = 0;
int c = 1/b;
System.out.println("Access element three :" + a[3]);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException
thrown :" + e);
}
catch (Exception e) {
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
b Write a Java program that simulates an age-verification CO3 BL4 [4M]
system. Create a custom exception InvalidAgeException and
throw it if the user's input age is less than 18. Include
a main method that prompts the user for their age, uses
a try-catch block to handle the exception, and prints a
meaningful message.
class InvalidAgeException extends Exception
{
InvalidAgeException(String msg)
{ super(msg);
}
}
class AgeVerification
{
public static void main(String[] args)
{
try {
int age = 16; // user input
if(age < 18) throw new InvalidAgeException("Age
must
be 18 or above!");
System.out.println("Access granted.");
}
catch(InvalidAgeException e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
Prepared By: Dr B.Veera Jyothi , IT Department.