0% found this document useful (0 votes)
7 views3 pages

Java Programming Exam Questions 2024

The document outlines the examination details for a Java Programming course, including instructions for answering questions and the structure of the exam. It consists of four questions covering various Java programming concepts, including method overloading, constructors, and object-oriented programming principles. Students are required to write Java programs and explain concepts in their answers, with specific marks allocated to each question.

Uploaded by

GDE The third
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)
7 views3 pages

Java Programming Exam Questions 2024

The document outlines the examination details for a Java Programming course, including instructions for answering questions and the structure of the exam. It consists of four questions covering various Java programming concepts, including method overloading, constructors, and object-oriented programming principles. Students are required to write Java programs and explain concepts in their answers, with specific marks allocated to each question.

Uploaded by

GDE The third
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

UNIVERSITY EXAMINATIONS: 2023/2024

EXAMINATION FOR DEGREE IN BACHELOR OF SCIENCE IN


INFORMATION TECHNOLOGY/ BUSINESS I.T/APPLIED
COMPUTING/ SOFTWARE DEV./INFO. SECURITY& FORENSICS
BIT 2204A/ BBIT 310 /BAC 2201/ BSD 2107/BISF 2201:
JAVA PROGRAMMING
PART TIME/FULL TIME/DISTANCE LEARNING
ORDINARY EXAMINATION
DATE: AUGUST, 2024 TIME: 2 HOURS
INSTRUCTIONS: Question One Is Compulsory, Choose Two Other Questions
QUESTION ONE (20 marks) Compulsory
a) When you compile a program written in the Java programming language, the compiler
converts the human-readable source file into platform-independent code that a Java Virtual
Machine can understand. What is this platform-independent code called? Explain why this
is necessary. (3marks)
b) Explain the part of the main method “public static void main (string []args)” (4marks)
c) Differentiate between method overloading and method overriding. (2marks)
d) State the main difference between the following terms used in Object Oriented
Programming
i. Object and classes (2marks)
ii. Encapsulation and data abstraction (2marks)
e) What are constructors? Giving an example, show why they are useful in java programming
(3marks)
f) Write a Java program which allow the user to input four integers it then output the
maximum of the four integers. (4marks)

QUESTION TWO (15 marks)


a) Write a java application that prints, on separate lines, you name, your birthday, your
hobbies and your favourites books. Label each piece of information in the output.
(5marks)
b) Write a Java program to input the price and quantity purchased of an item. The program
them computes the discount and the total amount to be paid. Assume a discount of 6%
(5marks)
c) Write a program that prompts the user for the radius and height of a solid cylinder.
Integrate into the program the functions find_SArea( ) and find_Vol( ) which respectively
calculates the total surface area and volume of the cylinder and returns their result to the
caller for display. Include an exception to trap negative value radius and height with
appropriate error messages. (5marks)

QUESTION THREE (15 marks)


a) A hotel is giving seasonal discount on the total amount to be paid by the person staying at
the time of check out. The charge for one day stay is Kshs 850.00 per room. The discount
will be given as per the following criteria:
Number of days stayed Discount on total amount
Up to 5 days 10 %
> 5 days and < = 10 days 15 %
> 10 days and < = 20 days 20 %
More than 20 days 25 %
Required:
Write a java program that will:
i. Input name of guest, total number of days stayed in the hotel. (4marks)
ii. Calculate the total amount to be paid and the discount amount. (6marks)
iii. Find the net balance to be paid excluding the discount. (5marks)

QUESTION FOUR (15 marks)


a) Describe what is meant by the term method over riding and comment on the importance of
method signatures in determining which method is overridden. Illustrate method overriding
with code written in java. (5marks)
b) Use the java code below to answer the questions that follow.
class atomClass
{
private:
int protons;
int neutrons;
int electrons;
protected:
static int electronCharge;
public:
atomClass();
atomClass(int p, int n, int e);
void setProtons(int p);
int getNeutrons();
};
i). If we wished to publically inherit from atom Class, explain which members would
be visible in the derived class, and state their designation. (3marks)
ii). Explain the purpose of a copy constructor, and propose how one might be
implemented for atomClass, assuming that we had a full suite of accessor functions.
(2marks)
c) Write the output of the following java inheritance code (3marks)
Class A{
Public void fun (ints){
[Link]. println(“int in A is:”+x);
}
}

Class B extends A{
Public void funz (int x, inty){
fun (6)
[Link]. println(“int in B is:”+x” and “+
y);
}
}

public class inherit {


public static void main (string [] args){
B obj=newB();
[Link](2,5);
}
}
d) Explain the difference between the outputs from program statements in I and II below.
(2marks)
I. String s;
[Link]("s = " + s);
II. String s = new String();
[Link]("s = " + s);

Common questions

Powered by AI

Encapsulation in Java is the process of wrapping data (variables) and code (methods) together as a single unit, typically by using private fields and providing public getter and setter methods. This promotes controlled access and modification. Data abstraction, on the other hand, is the principle of exposing only the needed details of an object and hiding the complex background processes. While encapsulation is about data security and hiding internal representations, abstraction focuses on showing only necessary features, leaving hidden the unnecessary parts .

In Java, method signatures, which include the method name and parameter list, are significant for method overriding because they determine which method will be executed at runtime. Overriding relies on having the same signature in both the base and derived class methods, allowing the derived class to provide specific behavior. For example, a parent class 'Animal' with a method 'speak()' can be overridden in a child class 'Dog', where 'speak()' could print 'bark', thus providing specific behavior for the 'Dog' class .

A Java program can compute discounts based on the length of stay in a hotel using if-else statements to determine discount rates for different stay durations. The program inputs the number of days a guest stays and calculates the discount based on predefined thresholds: 10% for up to 5 days, 15% for 6-10 days, 20% for 11-20 days, and 25% for more than 20 days. Once the discount is determined, the program multiplies the daily rate by the number of days, applies the discount, and computes the final bill .

In the 'public static void main(String[] args)' method of a Java program, 'public' is an access modifier allowing JVM to execute the method from anywhere. 'static' denotes that the method belongs to the class rather than an instance, which is essential because the main method serves as the entry point and needs to be accessible before any object instantiation. 'void' indicates that the main method does not return any value. The 'main' is the name of the method searched by JVM as the starting point of any Java application. 'String[] args' is an array of String objects, representing command-line arguments passed to the Java program .

In Object-Oriented Programming, a class is a blueprint or template for creating objects; it defines properties and behaviors. An object, however, is an instance of a class and represents an entity with a specific state and behavior defined by the class. Classes provide structure, while objects embody real-world entities based on that structure .

When using uninitialized strings in Java as in 'String s; System.out.println("s = " + s);', the output will result in a compilation error since 's' has not been initialized to any object or value. In contrast, if 's' is initialized like 'String s = new String(); System.out.println("s = " + s);', the output will be 's = ' because the empty string constructor initializes 's' to an empty string, thus preventing any error and producing a valid, albeit empty, output .

The platform-independent code in Java is called bytecode. It is necessary because it allows Java programs to be run on any device that has a Java Virtual Machine (JVM) without recompilation. This is a key feature for Java's portability, ensuring that applications written in Java can execute on any system, mitigating the need to modify the program for different platforms .

Method overloading in Java refers to defining multiple methods in the same class with the same name but different parameters (type or number). It increases the method's readability. On the other hand, method overriding occurs when a subclass has a specific implementation for a method already defined in its superclass with the same signature. It allows subclasses to provide specific functionality rather than relying on the parent class method .

Constructors are important in Java because they initialize new objects and establish initial values for their fields. Unlike regular methods, constructors have the same name as their class and do not have a return type, including void. For example, in a class 'Car', a constructor 'Car()' could set attributes like 'color' and 'model' upon creation of a car object. This ensures that all car objects have basic, valid configurations from inception .

A copy constructor might be implemented in Java to create a new object as a copy of an existing object, allowing for duplication of complex objects with reference variables or to ensure immutable copies. In the context of 'atomClass', a copy constructor would use parameters of another 'atomClass' object to copy primitive fields like 'protons', 'neutrons', and 'electrons', potentially enhanced by getter methods. This protects from shallow copying issues, preserving object integrity and preventing data from being altered unintentionally .

You might also like