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

Java Programming Exam Guidelines

This document contains exam instructions and questions for a Java programming exam. It outlines the exam format which has two sections - Section A contains two compulsory questions and Section B contains five questions where students must answer three. The questions cover topics like variables, data types, control flow, object-oriented programming concepts, and using classes and methods in Java code.

Uploaded by

Wil Son
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)
12 views3 pages

Java Programming Exam Guidelines

This document contains exam instructions and questions for a Java programming exam. It outlines the exam format which has two sections - Section A contains two compulsory questions and Section B contains five questions where students must answer three. The questions cover topics like variables, data types, control flow, object-oriented programming concepts, and using classes and methods in Java code.

Uploaded by

Wil Son
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

Code of Name of the Module Date of Exam Time of Set

the Module Exam

COM323 Programming in Java 21.06.2021 09:00 1

You are advised to read the following before answering the examination question.

1 Read each of the questions carefully before you answer.


2 Number the answers to the questions clearly before answering.
3 Answer all parts of a question at one place in continuous manner.
4 Please write as clearly as possible as illegible handwriting cannot be marked

This paper contains two parts; Section A and Section B. Section A is compulsory and
comprises two questions; Q.1 having five sub questions of four marks each is based on a case
study and Q.2 having 5 sub questions of 4 marks. Section B contains five questions having
two sub questions of ten marks each. Answer any three questions from section B.

Section A
Answer both questions
Carefully consider and examine the code below and then answer Q.1.
PUBLIC class Cuz{
String name = “CAVENDISH UNIVERSITY”;
public static main void(string[ ] args)
{
[Link]( I am as student at + name):

}
}

Q. 1
a) Would the program above execute? What would be the program’s exact output? (4 marks)
b) Why do programmers include comments in code? Briefly explain the two ways by which comments
could be inserted into a Java program. (4 marks)
c) What do you understand by the term debugging? Does the code above have syntax errors? If so, what
would you change to fix them? (4 marks)
d) What is a variable? List any 3 best practices to consider when naming variables. (4 marks)
e) List and briefly explain any 4 data types in Java. (4 marks)
Q. 2

a) Explain what you understand by the term ‘iteration’. When is iteration useful and list the 3 control
statements used for iteration in Java. (4 marks)
b) List 4 examples of logical operators in Java (4 marks)
c) Briefly explain the concept of the Java Virtual Machine (4 marks)
d) Explain all the steps involved from writing a java source code to getting the intended output on the
screen. (4 marks)
e) What is exception handling and why is it important in Java programming? (4 marks)

Section B
Answer any three questions
Q.3

a) Explain the differences between the public, private and protected access modifiers in Java. (10 marks)

b) Write a Java program that will display even numbers between 1 (one) and 16 (sixteen). Use either a for
or while loop for this. (10 marks)

Q.4

a) Write a Java program that will prompt a user to enter two numbers (of type int) and give the quotient.
Your program should give a remainder, if any, after dividing the second number (num2) into the first
(num1). Your program should follow mathematical rules. (10 marks)
b) By giving examples, explain the differences between local, instance and class/static variables. (10 marks)

Q. 5

a) What do you understand by the term “reserved” or “keywords” in the Java programming language? List
any eight (8) reserved words in the Java programming language (10 marks)
b) What is the 'new' keyword used for in Java? What is a class? Write a Java program with a method called
MessageDisplay( ) to be called in the main method. The MessageDisplay( ) method should simply contain
a single statement displaying the message “Java was awesome!” to the screen. (10 marks)

Q. 6

Consider and examine the excerpt of code below;


String country="ZAMBIA";
for(int i=[Link]()-1;i>=0;i--)
[Link]([Link](i));

[Link]();
a) Briefly explain what you understand the code above will accomplish. What output shall be produced?
(10 marks)
b) What is ‘concatenation’? Explain the concept of operator overloading and give a practical example of an
operator overload. (10 marks)

Q. 7

a) What is a method? What is a method’s return type? Write a Java program with a user-defined method
called getProd ( ) to return the product of two numbers. that will prompt a user to enter two numbers and
will return the product. (10 marks)
b) Explain with an example, how that the Scanner class is used to capture user input of type int, double
and String from a user. (10 marks)

Common questions

Powered by AI

The Java code will not execute as intended due to several syntax errors. The main method signature is incorrect; it should be 'public static void main(String[] args)'. Additionally, 'system.Out.println' should be 'System.out.println', and string concatenation is done improperly. After fixing these errors, the corrected code is: public class Cuz { String name = "CAVENDISH UNIVERSITY"; public static void main(String[] args) { System.out.println("I am a student at " + name); } } However, the attribute 'name' should be static because it is being used in a static context. The correct output would be 'I am a student at CAVENDISH UNIVERSITY'.

The code reverses the string "ZAMBIA" and outputs 'AIBMAZ'. This is achieved by iterating through the string from the end to the beginning using a for loop, printing each character in reverse order. This illustrates Java's versatility in string manipulation, specifically how native string methods like 'charAt()' and 'length()' can be combined with control structures to perform complex operations. It also highlights Java's ability to handle strings as immutable objects efficiently by creating new strings instead of modifying the original.

Exception handling in Java is important because it helps manage and respond to runtime errors in a controlled manner, allowing a program to continue execution or fail gracefully. Through exception handling, developers can prevent program crashes by catching and resolving errors like division by zero or null pointer references. This feature also simplifies debugging and enhances program stability, making it possible for a program to notify users of issues and take corrective actions automatically. Java implements exception handling through the try-catch-finally blocks and supports custom exceptions for specific error scenarios.

A variable in Java is a named memory location used to store data that can be manipulated by the program. Best practices for naming variables include: 1) Using meaningful names that clearly describe the variable's purpose, 2) Starting variable names with a letter or underscore but not with a number, and 3) Following camelCase convention for readability, where the first word is lowercase and each subsequent word starts with an uppercase letter, such as 'studentName'.

The 'new' keyword in Java is used to instantiate objects, allocating memory for them on the heap. It is used to create instances of classes, allowing the program to interact with the object's methods and attributes. Class instantiation is demonstrated in a simple program by defining a class with a method 'MessageDisplay()' and creating its instance in 'main'. For example: class HelloWorld { void MessageDisplay() { System.out.println("Java was awesome!"); } public static void main(String[] args) { HelloWorld hw = new HelloWorld(); hw.MessageDisplay(); } } This program creates an instance 'hw' of class 'HelloWorld' and calls 'MessageDisplay()', printing the message.

The Java Virtual Machine (JVM) plays a crucial role in executing Java programs by providing a platform-independent environment that interprets compiled Java bytecode. The JVM is responsible for loading, verifying, and executing bytecode, which allows Java programs to run on any device with a JVM installed, regardless of the underlying hardware architecture. This promotes Java's 'write once, run anywhere' capability. Additionally, the JVM manages memory through garbage collection, optimizing resource use during execution.

Debugging involves identifying and fixing errors in a program to ensure it runs correctly. In the provided Java code, debugging is required due to syntax errors including an incorrect main method signature, improper capitalization in 'System' and 'out', and incorrect use of string concatenation. To resolve these errors: change 'public static main void(string[ ] args)' to 'public static void main(String[] args)', 'system.Out.println' to 'System.out.println', and correct the string concatenation syntax to properly join the variable 'name' with the string literal. Therefore, a comprehensive understanding of Java syntax and error messages is fundamental to effective debugging.

In Java, local variables are declared within a method or block and are only accessible within that method, being destroyed once the method exits. Instance variables are declared within a class but outside any method, and each object of the class has its own copy. A class or static variable is shared among all instances of the class and is declared using the 'static' keyword. For example, in a class 'Car', 'mileage' can be a local variable within a method 'drive', 'color' an instance variable for each car object, and 'maxSpeed' a static variable as it is common for all cars of the class.

The process begins with writing Java source code in a text editor, saved with a .java extension. This source code is then compiled by the Java compiler (javac) into bytecode, which is stored in a .class file. The bytecode is platform-independent and can be interpreted by the Java Virtual Machine (JVM). The JVM uses a class loader to load the bytecode, a bytecode verifier to ensure it is secure and valid, and an interpreter or Just-In-Time (JIT) compiler to translate bytecode into machine code. Finally, the machine code is executed by the CPU, producing the desired output on the screen.

Comments are used in Java to enhance code readability and maintainability, making it easier for someone else (or the author at a later time) to understand the logic and purpose of the code. Comments can also be used to temporarily disable code during debugging. There are two main types of comments in Java: single-line comments and multi-line comments. Single-line comments are initiated with '//' and continue to the end of the line. Multi-line comments are enclosed between '/*' and '*/', which can span multiple lines.

You might also like