Professional Documents
Culture Documents
Department of BES-1
23SC1203-CTOOD INSEM-1 EXAM ANSWER KEY
Date of Exam: 14-03-2024
A.C.Y:2023-24, Even Semester
General Instructions:
• Acknowledge that the suggested answer key is a sample solution and may not
be the only correct approach.
• Recognize and value the different problem-solving approaches and logic used
by students.
• Award marks based on correctness and alternative correct solutions
irrespective of adherence to the suggested key.
SECTION – A (2M)
1. A) Define the term “abstraction” in the context of OOP? [2M]
Abstraction is a process of hiding the implementation details or unnecessary details and showing
only functionality or necessary details to the user.
For example, a Shape class may have abstract methods like calculateArea() and
calculatePerimeter(), allowing specific shapes like Circle and Square to implement these methods
according to their unique properties, without exposing the internal calculations to the user.
1. B) Differentiate between static method and instance method in Java? [2M]
Criteri
Static Method Instance Method
a
Ownership Belongs to the class itself Belongs to instances of the class
Invocation Invoked using class name Invoked using object of the class
Can only access static Can access both static and instance
Access to Fields
variables variables
Allocated memory each time an
Memory Allocated memory once at
object is created
Allocation class loading
Page 1 of 17
1. D) Explain Singleton Class? [2M]
The singleton pattern restricts class instantiation to one object, achieved by a private constructor
preventing external instantiation. A static method returns the instance, creating it if necessary,
while a static variable holds the single instance.
Page 2 of 17
1. F) Describe the ‘extends’ keyword in Java and its role in inheritance. [2M]
In Java 'extends' keyword allows a subclass to be linked to a superclass and inherit its properties
and methods. It encourages the reuse of code and makes it easier to create specialized classes by
building upon pre-existing ones.
SECTION – B (4M)
2. A) Develop a Java Program that prompts the user to enter a three digits integer and
determines whether it is palindrome or not?
import java.util.Scanner;
class TwoA
{
public static void main(String[] args)
{
2. B) Assume a class Book with the following attributes – ISBN (long), Title (String), Price
(double) with private access. Draw the class diagram and develop code. Write the accessor,
mutator methods and toString() method.
Page 3 of 17
class Book
{
long ISBN;
String Title;
double price;
long getISBN() {
return ISBN;
}
void setISBN(long i) {
ISBN = i;
}
String getTitle() {
return Title;
}
void setTitle(String t) {
Title = t;
}
double getPrice() {
return price;
}
class Employee
{
private String ID;
private String name;
private String designation;
private int experience;
private double salary;
Page 6 of 17
2. D) Identify the output of the following code
class Person
{
private String getInfo()
{
return “Person”;
}
public void printPerson()
{
System.out.println(getInfo());
}
}
Person
Person
SECTION-C (5M+6M)
3. A) An Armstrong number is a special kind of number in math. It’s a number that equals the
sum of its digits, each raised to a power. For example ,if you have a number like 153, its an
Armstrong number because 1^3+5^3+3^3 equals 153. Write a class Armstrong with 2 static
methods isArmstrong() to check whether a given number “n” is Armstrong or not and
displayArmstrong() to display all Armstrong numbers up to “n”. Access them using main()
method from the same class and use appropriate access specifiers?
package tgan;
import java.util.Scanner;
public class Armstrong {
private static int n;
private static boolean isArmstrong(int a) {
int s=0,temp=a,nd=(int)Math.log10(a)+1;
while(a>0) {
Page 7 of 17
s=s+(int)Math.pow(a%10, nd);
a=a/10;
}
if(s==temp)
return true;
return false;
}
private static void displayAllArmstrong() {
for(int i=1;i<=n;i++)
if(isArmstrong(i)==true)
System.out.println(i);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
displayAllArmstrong();
sc.close();
}
}
*****
****
***
**
*
package tgan;
import java.util.Scanner;
public class Pattern {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i,j,s,n=sc.nextInt();
for(i=1;i<=n;i++)
{
for(s=1;s<=i-1;s++)
System.out.print(" ");
for(j=1;j<=n-i+1;j++)
System.out.print("*");
System.out.println();
}
}
}
(OR)
4. A) The task is to read the number of inches of type double, through console, then convert it to
centimeters and display the result . Modularize to class level , draw the class diagram, and
Page 8 of 17
develop the logic in a static function.(HINT: 1 INCH= 2.54 CENTIMETERS)
package co1;
import java.util.Scanner;
// Class responsible for conversion logic
class Converts
{
// Static function to convert inches to centimeters
public static double inchesToCentimeters(double inches) //Method
{
return inches * 2.54;
}
}
// Main class
public class MainDemo
{
package p1;
import java.util.Scanner;
Page 9 of 17
public class Average {
public static void main(String[] x){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of Integers");
int n = sc.nextInt();
//Creating an array
int[] a = new int[n];
avg = avg/n;
System.out.println("Average of Given integers :: "+avg);
}
}
SECTION-D (5M+6M)
5. A) Draw class diagram for the following scenario-Develop a class Voter with ID, name, and
address as attributes. Address is a reference to Address class with city, state, country, and
pin code as attributes. Code the parameterized constructors, and toString() method in both
the classes. Develop the class VoterDemo with main () method to read details of Voter and
print in the following format: VOTER ID:12345, Name: ABC, Address-[City: Guntur,
State: Andhra Pradesh, Country: India, Pincode: 520078].
import java.util.Scanner;
class Voter {
private int id;
private String name;
private Address address;
Page 10 of 17
@Override
public String toString() {
return "VOTER ID: " + id + ", Name: " + name + ", Address-" + address;
}
}
class Address {
private String city;
private String state;
private String country;
private int pincode;
@Override
public String toString() {
return "[City: " + city + ", State: " + state + ", Country: " + country + ", Pincode:
" + pincode + "]";
}
}
public class EAVoterDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter voter ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter voter name: ");
String name = scanner.nextLine();
System.out.print("Enter city: ");
String city = scanner.nextLine();
System.out.print("Enter state: ");
String state = scanner.nextLine();
System.out.print("Enter country: ");
String country = scanner.nextLine();
System.out.print("Enter pincode: ");
int pincode = scanner.nextInt();
Address address = new Address(city, state, country, pincode);
Voter voter = new Voter(id, name, address);
System.out.println(voter);
scanner.close();
}
Page 11 of 17
5. B) Write a test program that prompts the user to enter 5 numbers, stores them in an array
list, and displays their sum.
import java.util.ArrayList;
import java.util.Scanner;
public class SumFromArrayList {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> numbers = new ArrayList<>();
// Prompt user for 5 numbers
for (int i = 0; i < 5; i++) {
System.out.print("Enter number " + (i + 1) + ": ");
numbers.add(scanner.nextInt());
}
scanner.close();
// Calculate the sum
int sum = 0;
for (int num : numbers) {
sum += num;
}
// Display the sum
System.out.println("The sum of the entered numbers is: " + sum);
}
}
(OR)
6. A) Draw the Class Diagram and code the class Mobile with following private attributes: model,
brand, warranty(type int) and price(type double). Code the accessors, mutators and
toString() method. Develop the main() method to test the functionality of the class
Page 12 of 17
Class Diagram ---------- 2 marks
<< +Mobile>>
-model:String
-brand:String
-warrenty:int
-price:double
~Mobile(String:m,String:b,int:w,double:p)
+getModel():string
+setModel(String:m):void
+getBrand():string
+setBrand(String:m):void
+getWarrenty():int
+setWarrenty(int:m):void
+getprice():double
+setprice(double:m):void
+toString(void):String
+main(String args[]):void
package p1;
public class Mobile {
}
public String toString()
{
return getModel()+" "+getBrand()+" "+getWarrenty()+" "+getPrice();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Mobile m1=new Mobile("narzo50","realme",2,20000);
Mobile m2=new Mobile("iphone14","Apple",2,100000);
System.out.println(m1);
System.out.println(m2);
}
6. B) Explain Polymorphism. Compare and contrast method overloading and method overriding
with examples
6marks
Page 14 of 17
Runtime Polymorphism( function overriding)
there are multiple functions with the same name but different parameters then these functions are
said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and
a change in the type of arguments.
The differences between Method Overloading and Method Overriding in Java are as follows:
Static binding is being used for Dynamic binding is being used for overriding
overloaded methods. methods.
The argument list should be different The argument list should be the same in method
while doing method overloading. overriding.
Page 15 of 17
Ex
package p1;
//function overloading example
public class funover {
}
public void add(int a,int b)
{
System.out.println(a+b);
}
Method overriding
In Java, Overriding is a feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its super-classes or parent classes.
When a method in a subclass has the same name, the same parameters or signature, and the same
return type(or sub-type) as a method in its super-class, then the method in the subclass is said
to override the method in the super-class.
Ex
//Function overriding
package function_overriding;
class A
{
public void FunA()
{
System.out.println("A");
}
}
public class B extends A{
Page 16 of 17
public void FunA()
{
System.out.println("B");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
B b = new B();
b.FunA();
Page 17 of 17