University of Zambia CSC 2000 Computer Programming Past Paper PDF 2021

Document Details

IngeniousKeytar

Uploaded by IngeniousKeytar

School of Natural Sciences, Department of Computer Science

2021

The University of Zambia

Tags

Java Programming Object-Oriented Programming Computer Science University Exam

Summary

This is a past paper for CSC 2000 Computer Programming at the University of Zambia, from November 2021. The paper contains questions covering topics such as Java programming, inheritance, arrays, loops, and Fibonacci sequences. This document is highly valuable for computer science students.

Full Transcript

# THE UNIVERSITY OF ZAMBIA ## School of Natural Sciences ### Department of Computer Science ## FINAL EXAMINATION ### CSC 2000 #### Computer Programming | Date: | 19th November 2021 | | Time: | 09:00hrs - 12:00hrs | | Duration: | 3 Hours | | Venue: | NSLT | ## Instructions 1. There are two (2) s...

# THE UNIVERSITY OF ZAMBIA ## School of Natural Sciences ### Department of Computer Science ## FINAL EXAMINATION ### CSC 2000 #### Computer Programming | Date: | 19th November 2021 | | Time: | 09:00hrs - 12:00hrs | | Duration: | 3 Hours | | Venue: | NSLT | ## Instructions 1. There are two (2) sections in this exam paper. 2. Only the java programming language should be used in this exam 3. Answer all the questions in Section A and choose any three (3) questions from Section B ## SECTION A. Short answers (40 marks) You are required to answer all questions in this section. 1. It is often argued that one can only claim to have grasped the key concepts of Object Oriented Programming if they understand inheritance. Shapes are often used to demonstrate inheritance. In this question you are required to develop an inheritance hierarchy that is built on a Polygon class. * Write code for the polygon base class which has abstract methods area() and perimeter(). [3 marks] * Write code for class Triangle, which extend the Polygon base class, with mathematically valid implementations of the area() and perimeter() methods. [6 marks] * Isosceles and Equilateral triangles are special kinds of triangles. An Isosceles triangle is a triangle that has two sides of equal length and Equilateral triangle is one with all sides being equal. Write classes for the two types of triangles exhibiting the appropriate inheritance relationships. [6 marks] * In a triangle, the sum of any two sides is always greater than the other side. The Triangle class must adhere to this rule. Create an Illegal TriangleException class, and modify the constructor of the Triangle class to throw an Illegal TriangleException when a triangle is created with sides that violate the rule. [5 marks] 2. * What is an Object and a Class? [4 marks] * Clearly explain what each part of the method declaration below implies. [4 marks] ```java public static void main(String args[]) ``` * How does Java achieve platform independence? [4 marks] * Briefly discuss any four key characteristics of Java. [4 marks] * What are the four access modifiers in Java [4 marks] ## SECTION B (40 marks) Answer any three (3) of the five (5) questions in this section. Each question carries 20 marks 1. You have been tasked to write code for a class called GradesCount. * What is an array? [2 marks] * The class reads a list of scores from the keyboard (integer numbers in the range 0 to 100). It prompts a user with a "---Please enter a score between 0 and 100 or -1 to quit " message each time before reading the next integer. [4 marks] * Store the scores in one array and the grades in a corresponding parallel array. Each score is graded as either A, B, C, D or F as follows: 90 to 100 = A, 80 to 89 = B, 70 to 79 = C, 60 to 69 = D, and 0 to 59 = F. [6 marks] * It has a method that outputs the total number of each type of grade i.e the number of As, Bs, Cs, Ds and Fs. [4 marks] * It also has a method that computes the average score and grade. [4 marks] * **For example, if the input is:** * 38 * 86 * 92 * 55 * 83 * 42 * 90 * -1 * **For example, if the input is:** * Total number of grades = 7 * Number of A = 2 * Number of B = 2 * Number of C = 0 * Number of D = 0 * Number of F = 3 * The A grades are: 92, 90 2. Consider the following problem from a requirement specification: * "If a customer enters a store with the intention of buying a toy for a child, then advice must be available within a reasonable time concerning the suitability of the toy for the child. This will depend on the age range of the child and the attributes of the toy. If the toy is a dangerous item, then it is unsuitable." * Identify the classes and their members (fields and methods/behaviours) from this requirement. Write a simple java program that simulates/implements the above specification. 3. More often than not, one is required to repeat a task until some condition is met or broken. For this most programming languages have constructs for looping. * What is a loop? [2 marks] * How are conditions related to loops? [2 marks] * Write code that demonstrates at least two different ways of implementing loops in Java. [4 marks] * Write a program that prints numbers from 1 to 10 on the screen. [4 marks] * A Fibonacci sequence is defined as a series of numbers in which each number ( Fibnacci number) is the sum of the two preceding numbers. The simplest is the series 0 1, 1, 2, 3, 5, 8, etc. This is the set of the first 7 numbers in the Fibonnaci sequence. Write a program to print a Fibonacci series of n terms where n is input by user. In our sample series n is 7. [8 marks] 4. Good programming requires that one develops conventional skills and norms. With such skills a good programmer can write code that is easier to understand thereby maintain by other programmers. * List all the Java primitive data types? [4 marks] * Write the commands you would use to compile and execute code contained in a file name "HelloWorld.java". [4 marks] * What are the simple rules to giving a valid identifier name? [3 marks] * What are the conventions used to name Classes, constants and variables? [3 marks] * Why is it important to properly comment your code? [2 marks] * What is wrong with the following code? [4 marks] ```java public class String Programs { static void main(String[10] args){ String s = "abc"; System.out.println(s); } } ``` 5. * Illustrate and explain state transitioning of a java thread. [6 marks] * In Java programming an object is said to have both state and behavior. What does Java use to define state and behavior? [4 marks] * While designing a program, a programmer notes that variable a needs to accessible to all classes in the same package only, variable b can be accessed from anywhere, variable c should only be accessed from inside the same class and variable d is accessible only to classes in the same package and to its subclasses. All variables are of type integer. Write code to declare the variables clearly showing the access modifiers. [4 marks] * Briefly explain the types of programming errors a programmer may encounter in Java. [6 marks] ## The end of this examination paper

Use Quizgecko on...
Browser
Browser