COMP 1131: Computer Programming I
Sample Exam Questions
1. What is a default constructor? (2 marks)
2. Suppose rng is a Random object. Use it to generate a random value between 10 and 20 for the variable num. (1 mark)
3. Translate the following switch-case logic into if-else logic. (2 marks)
switch(num){
case 3:
case 4:
System.out.println(“Three or Four”);
break;
default:
System.out.println(“Something else”);
}
4. Write a method that receives an array of integers as a parameter and returns the count of the number of 0 values found in the array. (3 marks)
5. Write a method called sign that takes in a number and returns 0 if the value is zero, 1 is the value is positive, and -1 if the value is negative. (2 marks)
6. In Assignment 2, you coded aclass to calculate the volume and surface area of a sphere from user input.
a. Draw the UML diagram to redesign this as an object-oriented class
with a constructor, get and set methods for the radius, get methods for the volume and surface area, and a toString() method to allow all the sphere attributes to be output by a driver. (5 marks)
b. Write the Java code for this design. (7 marks)
c. Write the driver code to instantiate a sphere of radius 1. Then change the radius to 5 and output the sphere. (3 marks)
7. Write a method that receives an array of String as a parameter and returns the length of the longest element of the array. (4 marks)
8. In computing, we are used to seeing the powers of 2 used a lot (i.e. 2, 4, 8, 16, 32, 64, …).
a. Write a recursive definition for finding thenth power of 2. (2 marks)
b. Implement this recursive definition as a method. (2 marks)
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。