2017 Final Exam Flashcards

1
Q

What does the term method-overloading mean? Give an example with two methods of your choice. [6 marks]

A

Method overloading allows creating multiple methods with the same name in a class, but with different parameters (number or data type).

It promotes code readability and reusability.

calculateArea(double radius)
for circles,

calculateArea(int length, int width) for rectangles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

iii) What are static methods in java and when do we use them? [5 marks]

A

Static methods in Java are part of the class itself, not an object of the class. You can call them without creating an object. They are useful for helper functions or functionality that doesn’t depend on a specific object’s state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the term encapsulation and discuss two advantages of encapsulation in Objected Oriented Programming.

A

refers to bundling data with methods that can operate on that data within a class.

information hiding from external classes helps the programmer keep control of your program.

information hiding from external classes helps prevent the program form ending up in any strange or unwanted states.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

iii) Explain with an example line of code the significance of the final modifier to a class, variable and method.

A

class MathUtil { // final class - cannot be inherited

public static final double PI = 3.14159; // final variable - constant value

public static final int add(int x, int y) { // final method - cannot be overridden
return x + y;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

iv) Find the error in the following method definition:
public static int half (double num){
double result = num/2.0;
return result;
}

A

The error is the return type. The method is declared to return an int (whole number), but the calculation and variable result are of type double (decimal).

fix:
public static int half(double num) {
return (int) (num / 2.0); // Cast the result to an int before returning
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

If the method returns int, int num = 2 what would be the result at the end?

A

1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain what are input or source streams and name all 2 root classes of input streams.

A

In Java, input streams represent sources of data that your program can read from. There are two root classes for input streams:

InputStream: This is the most general class for byte-oriented input streams. It provides basic methods for reading bytes from various sources like files, keyboards, or network connections.

Reader: This class is for character-oriented input streams. It deals with data in characters instead of raw bytes and offers methods for reading characters while handling character encoding.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain what is a char data type?

A

is a primitive data type used to store a single Unicode character. It occupies 16 bits (2 bytes) and can represent any character from a vast range, including letters, numbers, and symbols.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Character class?

A

is a wrapper class

for the primitive char data type. It provides functionality beyond storing a single character:

Object Representation: It allows you to treat a character as an object, useful in situations where objects are required.
Character Manipulation Methods: It offers various static methods for manipulating characters, such as checking case (uppercase/lowercase), digit or letter identification, and conversion between uppercase and lowercase
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

iii. Explain the following methods found in the Character class;
[10 marks]
a. isUpperCase()
b. toUpperCase()
c. isLowerCase()
d. toLowerCase()
e. isDigit()

A

isUpperCase(char ch)
: Checks if the character ch is an uppercase letter (A-Z) and returns true if yes, false otherwise.
toUpperCase(char ch): Converts the character ch to its uppercase equivalent if it’s a lowercase letter (a-z). Returns the original character if it’s already uppercase or not a letter.
isLowerCase(char ch): Checks if the character ch is a lowercase letter (a-z) and returns true if yes, false otherwise.
toLowerCase(char ch): Converts the character ch to its lowercase equivalent if it’s an uppercase letter (A-Z). Returns the original character if it’s already lowercase or not a letter.
isDigit(char ch): Checks if the character ch is a digit (0-9) and returns true if yes, false otherwise.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

iv. Differentiate between the following String class methods;
a. Equals() and equalsIgnoreCase(); [2 marks]
b. toUpperCase() and toLowerCase() methods [2 marks]
c. The endsWith() and startsWith() [2 marks]

A

a. equals() vs. equalsIgnoreCase()

equals(): Checks for exact string equality, considering both case and characters.
equalsIgnoreCase(): Checks for string equality, ignoring the case of characters.

b. toUpperCase() vs. toLowerCase()

toUpperCase(): Converts all characters in the string to uppercase.
toLowerCase(): Converts all characters in the string to lowercase.

c. endsWith() vs. startsWith()

endsWith(String suffix): Checks if the string ends with the specified suffix.
startsWith(String prefix): Checks if the string starts with the specified prefix.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Name 2 options plus the Java class of reading keyboard input into your program.

A

Scanner class
Console

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A

java.util.Scanner;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

i) What is an I/O stream?

A

An I/O stream in Java is a pathway for data to flow between your program and external sources/destinations (files, devices, etc.) for reading or writing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

ii) A stream can represent many different kinds of sources and destinations. Name any 4 sources or destinations.

A

Files
Network connections
Standard input (keyboard)
Memory arrays

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

iii) Streams supports many kinds of data. Give any 4 examples

A

Bytes (for raw data)
Characters (for text)
Objects (custom data structures)
Primitive data types (int, double, etc.) (through boxing/unboxing)

17
Q

Name the 2 general stream types

A

Byte Streams
: Deal with data in bytes (8-bit units), suitable for non-textual data like images, videos, etc.

Character Streams: Handle data in characters (16-bit Unicode), ideal for textual data like documents, code, etc.

18
Q

v) Give 2 differences and 2 similarities of the byte and characters streams

A

Differences between Byte and Character Streams:

Data Unit:
    Byte streams: Operate on individual bytes (8-bit units).
    Character streams: Work with characters (16-bit Unicode units).
Use Case:
    Byte streams: Suitable for raw, binary data (images, videos).
    Character streams: Ideal for text-based data (files, code).

Similarities between Byte and Character Streams:

Sequential Access: Both read or write data sequentially, one unit at a time.
Abstraction Layer: Both provide an abstraction layer, hiding the underlying details of data source/destination.
19
Q

vi) Explain what is abstract class in Java?

A

An abstract class in Java is a class that can’t be directly instantiated (creating objects). It acts as a blueprint for subclasses. It can have:

Abstract methods: Methods without a body, forcing subclasses to implement them.
Regular methods: Methods with implementation that subclasses can inherit.

It promotes code reusability and defines a common structure for subclasses.

20
Q

vii) Show with a sample class header how an abstract class is declared in Java

A

public abstract class Animal {
// … abstract methods and/or regular methods
}

21
Q

viii) Explain with a sample code how a class can be prevented from being extended in Java.

A

public final class MathUtil {
// … methods and variables
}