You are on page 1of 10

a

b. [3 marks] Briefly define the following:


i. Local variable
ii. Expression
iii. Array

i. A local variable is a variable that is declared and can only be used within the block of code in which it is defined. Local
variables are only accessible within the function or block of code in which they are defined and are not visible to other
functions or blocks of code.

ii. An expression is a combination of variables, operators, and values that evaluates to a single value. Expressions can be used
to perform calculations, assign values to variables, or evaluate conditions in control statements.

iii. An array is a data structure that stores a collection of items of the same type in a contiguous block of memory. Arrays are
indexed, which means that each element in the array can be accessed using a numerical index. Arrays are useful for storing
and manipulating large sets of data, such as lists of names or numbers.

5D. Distinguishe between method overriding and method overloading

Method overriding occurs when a subclass has a method with the same name and signature as a method in the superclass.
When this happens, the method in the subclass is said to override the method in the superclass. When the method is called on
an instance of the subclass, the subclass's implementation of the method is executed instead of the superclass's
implementation.

Method overloading occurs when a class has multiple methods with the same name but different signatures. The signature of
a method consists of the method name and the number and types of its arguments. When a method is called, the Java runtime
selects the correct implementation of the method based on the number and types of the arguments passed to the method.

Briefly define the following terms:


i. Method signature
ii. Array
iii. Typecasting
iv. Variables and Reference Variables

i. Method signature: A method signature is a part of a method declaration that specifies the name of the method and the type,
number, and order of its parameters. It does not include the method body or any other information.

ii. Array: An array is a data structure that stores a collection of items in a single place, typically in a contiguous block of
memory. Arrays are indexed, which means that you can access the individual elements of an array by using their position in
the array, starting with 0 for the first element.

iii. Typecasting: Typecasting is the process of converting a value of one data type into another data type. This is often
necessary when you need to use a value in a context where a different data type is expected.

iv. Variables and Reference Variables: A variable is a named location in memory that stores a value. A reference variable is a
special type of variable that stores a reference to an object rather than the object itself. When you use a reference variable to
access an object, you are actually using the reference to the object rather than the object itself.

Page 3 of 3
1. Write a Java Program that can serve as a Math Multiplication Learning Tool (MMLT). The program
should generates 20 random multiplication questions involving two integers between 1 and 20 for
user’s responses. The format of the questions to be generated are as follows:
What is 3 * 5? 15
You are correct!

What is 2 * 3? 4
You are wrong! 2 * 3 is 6.
The program should display the total correct count at the end

Page 3 of 3
a
1. [3 marks] Write a program of your choice that features the concepts of string and character processing

This program prompts the user to enter a string, and then it processes the string character by
character. If a character is a digit, it increments the digit count. At the end, it displays the
number of digits in the string.

Page 3 of 3
1. [5 marks] Some Websites impose certain rules for passwords. Write a method that checks whether a
string is a valid password. Suppose the password rules are as follows:
A password must have at least 10 characters.
A password can consists of only letters, digits and an underscore.
A password must contain at least 3 digits

This method first checks if the password has at least 10 characters. If not, it returns false. Then, it
processes the password character by character. If a character is not a letter, digit, or underscore, it
returns false. If a character is a digit, it increments the digit count. At the end, it returns true if the digit
count is at least 3, and false otherwise.

Page 3 of 3
1. A method that declares an array of 50 integers, initializes each element with a pronic number as its position to
the square of its index, and set the remaining elements to a random integer less than or equals to their
positions. The method should finally return the sum and average of all the elements

This method declares an array of 50 integers and a Random object. It then initializes each element with a
pronic number if its position is less than the length of the array, and sets the remaining elements to a
random integer less than or equal to their positions. Finally, it calculates the sum and average of all the
elements and prints them. The method returns the resulting array.
a
b. Explain (with examples, where possible) the usage and importance of each of
the following as relates to java:
i. package
ii. import statement
iii. Scanner class

i. A package in Java is a way to organize your Java code. It helps to prevent naming conflicts and makes it easier to locate the
relevant code when it is needed. For example, the java.util package includes the Scanner class, which is used for reading input
from the user. You can create your own packages in Java by using the package keyword followed by the name of the package.

ii. The import statement is used to include classes from other packages in your Java code. For example, you might use the
following import statement to include the Scanner class in your code:
import java.util.Scanner;
This would allow you to use the Scanner class without having to specify the full package name each time. The import
statement is useful because it can save you a lot of typing and make your code easier to read.

iii. The Scanner class is a part of the java.util package and it is used for reading input from the user. It is a very useful class that
allows you to read data from the user through the command line or from a file.

Clearly explain the following statement in three (3) shorts but precise points:
int[] numbers = new int[5];

1. int[] numbers declares a variable numbers that will hold an array of integers.
2. new int[5] creates a new array of integers with a length of 5.
3. The assignment int[] numbers = new int[5] creates a new array of integers with a length of 5 and assigns it to the numbers
variable.

Page 3 of 3
a
b. Distinguish between the following:
i. Repetition and Selection Control Structures
ii. Pass-by-value and Pass-by-reference
iii. Program and Programming
iv. Implicit and Explicit Typecasting

i. Repetition and Selection Control Structures:


Repetition control structures, also known as looping or iteration control structures, allow a program to execute a statement
or group of statements multiple times. Examples of repetition control structures include for loops and while loops.
Selection control structures, also known as branching or decision control structures, allow a program to execute different
statements depending on the evaluation of a condition. Examples of selection control structures include if statements and
switch statements.

ii. Pass-by-value and Pass-by-reference:


In pass-by-value, the value of an argument is passed to a function. The function operates on a copy of the value and any
changes made to the value within the function have no effect on the original value outside of the function.
In pass-by-reference, a reference to the memory location of an argument is passed to a function. The function operates on
the original value stored at that memory location and any changes made to the value within the function are reflected in the
original value outside of the function.

iii. Program and Programming:


A program is a set of instructions that tell a computer what to do. It can be written in any programming language, such as C++
or Python.
Programming is the process of creating a program. It involves designing the logic of the program, writing the code in a
programming language, and testing and debugging the program.

iv. Implicit and Explicit Typecasting:


Implicit typecasting, also known as automatic typecasting, is the process of converting a value from one data type to another
without the use of a typecasting operator. This is done automatically by the compiler when it is necessary for the value to be
used in an operation or assignment.
Implicit typecasting can potentially result in data loss if the target data type is not large enough to hold the value being
converted.
Explicit typecasting, also known as manual typecasting, is the process of converting a value from one data type to another
using a typecasting operator. The typecasting operator specifies the target data type to which the value should be converted.
Explicit typecasting allows the programmer to control the type conversion and ensure that the value is converted to the
desired data type. It also allows the programmer to convert values that may not be compatible with implicit typecasting.

Page 3 of 3
a
b. Suppose we have the following statements:
String s1 = “abcd”;
String s2 = “efgh”;
Is (s1 == s2) the same as s1.equals(s2)? Clearly justify your answer.

No, (s1 == s2) is not the same as s1.equals(s2).


The == operator is used to compare the reference values of two objects. It returns true if both objects point to the same
memory location, and false if they are pointing to different locations.
On the other hand, the .equals() method is used to compare the contents of two objects. It returns true if the contents of the
objects are equal, and false if they are not equal.
For example:
String s1 = "abcd"; String s2 = "efgh";
(s1 == s2) would return false, because s1 and s2 are pointing to two different objects in memory.
s1.equals(s2) would also return false, because the contents of the objects are different.
It is generally recommended to use the .equals() method to compare the contents of two objects, rather than the == operator.

Expatiate on the following statements diagram required "string in java is an object. strings are immutable and java
creates an interned object for string literals with the same character sequence"

In Java, a string is an object that represents a sequence of characters. The Java String class is defined in the java.lang
package, and it provides various methods for manipulating strings.
One important property of strings in Java is that they are immutable, which means that once a string object is created, its
value cannot be changed. For example, consider the following code:
String s = "hello";
s.toUpperCase();
Even though the toUpperCase() method returns a new string with all the characters in uppercase, it does not modify the
original string. Instead, it creates a new string object with the modified characters and returns a reference to it. The original
string remains unchanged.
Another important property of strings in Java is that they are interned, which means that the Java runtime maintains a pool of
strings in memory and reuses them when possible. When you create a string literal in your code, the Java compiler checks the
string pool to see if an equivalent string already exists. If it does, the compiler uses the reference to the existing string instead
of creating a new one. For example:
String s1 = "hello";
String s2 = "hello";
In this case, the Java compiler will not create two separate strings in memory. Instead, it will create a single string object and
assign its reference to both s1 and s2. This can help save memory and improve performance.
To summarize, in Java, strings are objects that represent a sequence of characters. They are immutable, which means their
value cannot be changed once created, and they are interned, which means the Java runtime reuses existing string objects
whenever possible.

distinguish between class and object

In object-oriented programming, a class is a template or blueprint for creating objects. It defines the properties and behaviors
that an object of the class will have. An object, on the other hand, is an instance of a class. It represents a specific entity with
its own set of characteristics and behaviors, which are defined by the class.
In general, a class is a way of organizing and encapsulating data and behavior, while an object is a specific instance of a class
with its own unique characteristics and behavior

Page 3 of 3
a
Define an array.
Clearly explain the following statement:
int x[] = new int[10];
Consider the following statement:
int x[] = {1,2,4,5,6,7};
how can the elements of array x be copied into an array say y?

In the first statement: int x[] = new int[10];


This creates an array of integers called x with a size of 10, meaning it can hold 10 integer elements. The array is initialized with
the default value of 0 for each element.
In the second statement: int x[] = {1,2,4,5,6,7}; This creates an array of integers called x and initializes it with the given values:
1, 2, 4, 5, 6, 7.

To copy the elements of array x into another array called y, you can use the System.arraycopy method:
int[] y = new int[x.length];
System.arraycopy(x, 0, y, 0, x.length);

This will copy all the elements from x into y. The first two arguments specify the source array (x) and the starting index of the
copy (0). The third argument specifies the destination array (y) and the fourth argument specifies the starting index in the
destination array. The final argument specifies the number of elements to copy.

2- Identify and explain the errors in the following code:


Public class ClassA{
public static void method1(){
double x = 1.8;
int y = x;
Method2();
}
public void method2(){
System.out.println(‘Ina kwana’);
return 0;
}
}

1. The class name and the filename do not match. In Java, the name of the file should be the same as the name of the public
class defined inside the file.
2. The keyword 'public' is written in lowercase. In Java, the keywords 'public', 'class', and 'void' should be written in
uppercase.
3. There is a missing curly brace after the declaration of method1().
4. The assignment of a double value to an integer is not allowed. This will cause a compile-time error.
5. The method2() method is called without specifying the class name. In Java, when calling a method from another method
in the same class, you should use the 'this' keyword.
6. The method2() method has a return type of void, but it has a return statement with a value of 0. This will cause a compile-
time error.
7. The single quotes around the string 'Ina kwana' are invalid. In Java, you should use double quotes to denote a string
literal.

Page 3 of 3
a
The correct code

public class ClassA {


public static void method1() {
double x = 1.8;
int y = (int) x;
this.method2();
}
public void method2() {
System.out.println("Ina kwana");
}
}

1. [15 marks]
a. [6 marks] While inspecting a code written by a friend of yours, you suddenly realized that, itis extremely
difficult to follow, talk less of maintenance. Write some comprehensive paragraphs for him and perhaps others,
pointing out that, like any other form of development process, software development requires the following of
some well-defined protocols/standards for an effective and sound outcome. The paragraphs should
unambiguously explain the protocols software developers need to follow in getting rid of such difficult to follow
and maintained spaghetti codes

Page 3 of 3

You might also like