0% found this document useful (0 votes)
27 views

2021 Java

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)
27 views

2021 Java

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
You are on page 1/ 5

a) Explain the following statement in your own words.

“Java is a high-level, platform-independent


programming language in IT world”.

Java is a highly readable and user-friendly programming language, especially when compared to
machine languages. It uses a compiler to convert its human-readable source code into
bytecode, which is an intermediate form of code. This bytecode is then executed by the Java
Virtual Machine (JVM), which translates it into machine language specific to the operating
system being used. Since there are different JVM implementations for different operating
systems, the same Java source code can be run on multiple platforms without modification.
This is how Java achieves its platform-independent nature, allowing developers to "write once,
run anywhere.”

b) i) Which of the following statements can use to create objects of above classes in Java. (Mention
the numbers)

(a) A obj = new A()

(b) B obj = new B()

(c) B obj = new A()

(d) A obj = new B()

(a), (b),(d)

ii) What is the suitable keyword to fill the blank if you can use to call the Parent class constructor?

super()

c) List down the three different variable types in Java and describe each of them using an example.

Boolean can only two different value one is true and other is false.

boolean isChecked = true;

Byte is the smallest numeric data type that can hold values -128 to 127

Byte small_num = 10;

Float is used for storing decimal point values with a precision of up to 7 digits and occupies 4 bytes of
memory.

Float decimalValue = 10.5f;


02) a) UML is a modern approach to modeling and documenting software. There are different diagram
types which can be used for several purposes.

i) What is the purpose of having so many diagram types to design a software?

To get comprehensive idea about software artifacts by looking at in many different views.

ii) Choose the correct statements related to parts of class diagrams in below.

(a) A book must have at least one page.

(b) A book can be authored by one author only.

(c) An author must have at least one book.

(d) A page can be part of one or many books.

(e) Many books can be authored by one author.

Answer is (a) and (e)

2)

(a) The attribute “id” is privately accessed.

(b) The attribute “name” is publicly accessed.

(c) getName() accept a parameter type String.

(d) isActive() is a protected method.

(e) isActive() is a private method.

Answer is (a) and (e)

iii) Describe the difference between Structural Modelling and Behavioral Modelling providing
examples for each type.

Structural Modeling (package diagram, class diagram, component diagram)

 Structural modeling captures the static features of a system.


 It is widely used in the documentation of software architecture.
 Presents an outline for the system.

Behavioral Modelling (Activity diagram, state machine diagram, use case diagram)

• Behavioral diagrams portray a dynamic nature of a system.

• It represents the behavior of a system, which describes the functioning of the system.
iv)

Write a java method called sumOfDigits that accepts an integer array as a parameter and returns the
sum of all the values in the array.

public int sumOfDigits(int[] arr) {


int total = 0;
for (int num : arr) {
total += num;
}
return total;
}

03) a) i) Write a Java code to implement following class diagram.


class Student {
private String name;
private double gpa;

Student() {

Student(String name) {
this.name = name;
}

public double getGpa() {


return this.gpa;
}
}

class UnderGraduateStudent extends Student{

class GraduateStudent extends Student{

b) i) What kind of exception can be occurred at the execution of following code?

ArithmeticException

ii) Rewrite the above code suggesting a way to handle that exception.
public class Demo {
public static void main(String[] args) {
try {
int a = 20, b = 0;
int c = a / b;
System.out.println("Result = " + c);
} catch (ArithmeticException e) {
e.printStackTrace();
}
}
}

iii) Explain the use of throw and throws keywords in exception handling with examples.

throw and throws both keywords use to counter runtime exceptions throw keyword us in a code block
or inside a method while throws use in method declaration .
public void divide(int a, int b) throws ArithmeticException {
if (b==0){
throw new ArithmeticException("can't divide by zero");
}
System.out.println(a/b);
}

4) i) What is Object Serialization?

Object Serialization is the process of converting an object in Java into a format (specifically, a byte
stream) that can be easily saved to a file or sent over a network. Later, this byte stream can be
converted back into the original object, a process known as deserialization.

ii) Given below is the equation to find the surface area of a circle.

A=πr2

a. Identify the suitable variables and constants with suitable datatypes.


double PI
double r

double area

b. Write a Java program to read the radius (r) value from console input and print the result in
proper manner.
public static final double PI = 3.141;
double radius = 0.0;
public static void areaCalc(double radius) {
radius = 5.0;
double area = PI * radius * radius;

System.out.println("The area of the circle with radius " + radius + " is:
" + area);
}

b) i) Describe the difference between Centralized and Distributed version controlling systems.

CVCS has a single main repository while DVCS can have copy of main repository for everyone

CVCS has limited access to commit history (only for central server) while DVCS has full access to commit
history

CVCS has more complex merging and branching process while DCVS has flexible merging and branching
facilities.

ii) Rewrite the following code using best coding practices.


public class Employees {
private List<Employee> employees; // Use generics for type safety
private int employeeCount; // Follow camelCase naming convention

// Getter method for the employees list


public List<Employee> getEmployees() {
// Initialize only when required
if (employees == null) {
employees = new ArrayList<>();
}
return employees;
}
}

You might also like