Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

Module3A Assessment

sample exam

Uploaded by

sjctrags
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Module3A Assessment

sample exam

Uploaded by

sjctrags
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Module 3-A Java Programming Concepts

Assessment: Theories and Exercises


Read carefully each item below. Each item below requires all programming concepts mentioned
in the previous discussions and demos. Follow Java Coding Standard seriously and follow all the
needed requirements. All outputs must be readable, presentable and clear. Not following
instructions will result to series of deductions.
Note: Project name must be <LastName>Ex3A and the official package is
org.alibata.training.codes.
Duration: 2 hours

A. Theoretical Assessment Open your notes and read each item carefully. There are few items
that may have more than one answer. This is to test also your code reading skills. Mind your
work. (2 pts per ans.)
1. What will happen if you try to compile and run the following code?
public class MyClass {

public static void main(String arguments[]) { }


public void amethod(String[] arguments) { }
}
A. An error can't make static reference to void method
B. An error method main not correct
C. An error array must include parameter
D. A method must be declared with String

2. What will happen if you try to compile and run the following code?
public class Q {

public static void main(String argv[]){


int anar[]=new int[5];
System.out.println(anar[0]); }
}
A. Error: anar is referenced before it is initialized
B. null
C. 0
D. 5

3. What is the output of this program?


class string_class {
public static void main(String args[]) {
String obj = "hello";
String obj1 = "world";
String obj2 = "hello";
System.out.println(obj.equals(obj1) + " " + obj.equals(obj2));
}
}
A. false false
B. true true
C. true false
D. false true

Alibata Business and Technology Solutions Inc. Page 1


4. Which of these is a process of converting a simple data type into a class?
A. type wrapping
B. type conversion
C. type casting
D. None of the Mentioned

5. Given the code fragment:


SimpleDataFormat sdf;

Which code fragment displays the three-character month abbreviation?

A. SimpleDateFormat sdf = new SimpleDateFormat ("mm", Locale.UK);


System.out.println ("Result:" + sdf.format(new Date()));
B. SimpleDateFormat sdf = new SimpleDateFormat ("MM", Locale.UK);
System.out.println ("Result:" + sdf.format(new Date()));
C. SimpleDateFormat sdf = new SimpleDateFormat ("MMM", Locale.UK);
System.out.println ("Result:" + sdf.format(new Date()));
D. SimpleDateFormat sdf = new SimpleDateFormat ("MMMM", Locale.UK);
System.out.println ("Result:" + sdf.format(new Date()));

6. Analyze the following code.


1. import java.util.*;
2. public class Test {
3. public static void main(String[] args) {
4. Calendar[] calendars = new Calendar[10];
5. calendars[0] = new Calendar();
6. calendars[1] = new GregorianCalendar();
7. }
8. }

A. The program has a syntax error on Line 4 because java.util.Calendar is an


abstract class
B. The program has a syntax error on Line 6 because Calendar[1] is not of a
GregorianCalendar type
C. The program has a syntax error on Line 5 because java.util.Calendar is an
abstract class
D. The program has no syntax errors

7. What is displayed on the console when running the following program?


class Test {
public static void main(String[ ] args) {
try {
method();
System.out.println(ʺAfter the method callʺ);
}
catch (NumberFormatException ex) {
System.out.println(ʺNumberFormatExceptionʺ);
}
catch (RuntimeException ex) {
System.out.println(ʺRuntimeExceptionʺ);
}
}
static void method() {
String s = ʺ5.6ʺ;

Alibata Business and Technology Solutions Inc. Page 2


Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println(ʺWelcome to Javaʺ);
}
}
A. The program displays NumberFormatException
B. The program displays NumberFormatException followed by after the method call
C. The program has a compilation error
D. The program displays NumberFormatException followed by RuntimeException
E. The program displays RuntimeException

8. What exception type does the following program throw?


public class Test {
public static void main(String[ ] args) {
Object o = new Object();
String d = (String)o;
}
}
A. StringIndexOutOfBoundsException
B. ClassCastException
C. ArrayIndexOutOfBoundsException
D. ArithmeticException
E. No exception

9. What is displayed on the console when running the following program?


class Test {
public static void main(String[ ] args) {
try {
method();
System.out.println(ʺAfter the method callʺ);
}
catch (RuntimeException ex) {
System.out.println(ʺRuntimeExceptionʺ);
}
catch (Exception ex) {
System.out.println(ʺExceptionʺ);
}
}
static void method() throws Exception {
try {
String s = ʺ5.6ʺ;
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
System.out.println(ʺWelcome to Javaʺ);
}
catch (RuntimeException ex) {
System.out.println(ʺRuntimeExceptionʺ);
}
catch (Exception ex) {
System.out.println(ʺExceptionʺ);
}
}

Alibata Business and Technology Solutions Inc. Page 3


}
A. The program has a compilation error
B. The program displays Exception followed by RuntimeException
C. The program displays RuntimeException followed by After the method call
D. The program displays RuntimeException twice
E. The program displays Exception twice

10. A method must declare to throw ________.


A. Error
B. RuntimeException
C. checked exceptions
D. unchecked exceptions

11. Which of the following is not an advantage of Java exception handling?


A. Java separates exception handling from normal processing tasks
B. Exception handling simplifies programming because the error-reporting and error-
handling
code can be placed at the catch block
C. Exception handling makes it possible for the callerʹs caller to handle the exception
D. Exception handling improves performance

12. Which statements are most accurate regarding the following classes?
class A {
private int i;
protected int j;
}
class B extends A {
private int k;
protected int m;
// some methods omitted
}
A) In the class B, an instance method can only access k, m
B) In the class B, an instance method can only access i, j, k, m
C) In the class B, an instance method can only access j, m
D) In the class B, an instance method can only access j, k, m

13. What is displayed on the console when running the following program?
class Test {
public static void main(String[ ] args) {
try {
System.out.println(ʺWelcome to Javaʺ);
int i = 0;
int y = 2/i;
System.out.println(ʺWelcome to HTMLʺ);
}
finally {
System.out.println(ʺThe finally clause is executedʺ);
}
}
}
A. The program displays three lines: Welcome to Java, Welcome to HTML, The finally
clause is executed.
B. Welcome to Java.

Alibata Business and Technology Solutions Inc. Page 4


C. Welcome to Java followed by The finally clause is executed in the next line.
D. None of the above.

14. Given:

class Plane {
static String s = "-";
public static void main(String[] args) {
new Plane().s1();
System.out.println(s);
}

void s1() {
try { s2(); }
catch (Exception e) { s += "c"; }
}

void s2() throws Exception {


s3(); s += "2";
s3(); s += "2b";
}

void s3() throws Exception {


throw new Exception();
}
}
What is the result?

A. - B. -c C. -c2 D. -2c E. -c22b F. -2c2b G. -2c2bc


H. Compilation fails

15. Given the following piece of code:

public interface Guard{


void doYourJob();
}
abstract public class Dog implements Guard{}

which of the following statements is correct?

A. This code will not compile, because method doYourJob() in interface Guard must be
defined abstract
B. This code will not compile, because class Dog must implement method doYourJob()
from interface Guard
C. This code will not compile, because in the declaration of class Dog we must use the
keyword extends in stead of implements
D. This code will compile without any errors

16. Given these classes:

public class Person{


public void talk(){ System.out.print("I am a Person "); }
}

Alibata Business and Technology Solutions Inc. Page 5


public class Student extends Person {
public void talk(){ System.out.print("I am a Student "); }
}

what is the result of this piece of code:


public class Test{
public static void main(String args[]){
Person p = new Student();
p.talk();
}
}
A. I am a Person
B. I am a Student
C. I am a Person I am a Student
D. I am a Student I am a Person

17. Given the following piece of code:

class SalaryCalculationException extends Exception{}

class Person{
public void calculateSalary() throws SalaryCalculationException {
//...
throw new SalaryCalculationException();
//...
}
}

class Company{
public void paySalaries(){
new Person().calculateSalary();
}
}
Which of the following statements is correct? (2 answers)

A. This code will compile without any problems


B. This code will compile if in method paySalaries() we return a boolean instead of
void.
C. This code will compile if we add a try-catch block in paySalaries()
D. This code will compile if we add throws SalaryCalculationException in the
signature of method paySalaries()

18. Given the following piece of code:

class Person{ public void talk(){} }

public class Test{


public static void main(String args[]){
Person p = null;
try{
p.talk();
} catch(NullPointerException e){
System.out.print("There is a NullPointerException. ");
} catch(Exception e){
System.out.print("There is an Exception. ");
}
System.out.print("Everything went fine. ");
}
}

Alibata Business and Technology Solutions Inc. Page 6


what will be the result?

A. If you run this program, the outcome is: There is a NullPointerException.


Everything went fine.
B. If you run this program, the outcome is: There is a NullPointerException.
C. If you run this program, the outcome is: There is a NullPointerException. There is
an Exception.
D. This code will not compile, because in Java there are no pointers.

19. How can you serialize an object?


A. You have to make the class of the object implement the interface Serializable
B. You must call the method serializeObject() (which is inherited from class Object)
on the object
C. You should call the static method serialize(Object obj) from class Serializer, with
as argument, the object to be serialized
D. You don’t have to do anything, because all objects are serializable by default

20. Which statements about IO are correct (2 answers)?


A. OutputStream is the abstract superclass of all classes that represent an
outputstream of bytes
B. Subclasses of the class Reader are used to read character streams
C. To write characters to an outputstream, you have to make use of the class
CharacterOutputStream
D. To write an object to a file, you use the class ObjectFileWriter
B. Exercises.
1. The Commission on Higher Education (CHED) has opened a bidding for a national
infrastructure project that will compute the total standing of a student in one section at the
end of every semester. The project is a software application that will feed on text files
containing the names of the students plus the scores incurred by each per subjects. A sample
text file is show below:
Jimmy 89 90 77 72
Anna 50 45 89
Rhea 100 99 77
Jun 55 68

The result of the application will be:


Rhea 92.00
Jimmy 82.00
Jun 61.50
Anna 61.33

A list of students with the averages of each based on the number of grades sorted in
descending order. Use old IO APIs and some arrays to implement the following project. All
averages must be in 2 decimal places with RoundingMode FLOOR.
2. For the next four problems, consider the task of representing types of tickets to school
campus events. Each ticket has a unique number and a price. There are three types of
tickets: walk-up tickets, advance tickets, and student advance tickets. Write a UML design
first and implement your design afterwards.
 Walk-up tickets are purchased the day of the event and cost $50.
 Advance tickets purchased 10 or more days before the event cost $30, and advance
tickets purchased fewer than 10 days before the event cost $40.

Alibata Business and Technology Solutions Inc. Page 7


 Student advance tickets are sold at half the price of normal advance tickets: ones 10
days early cost $15, and fewer than 10 days early cost $20.

a) Implement a class named Ticket that will serve as the superclass for all
three types of tickets. Define all common operations in this class, and
specify all differing operations in such a way that every subclass must
implement them. No actual objects of type Ticket will be created: each
actual ticket will be an object of a subclass type. Define the following
operations:
 The ability to construct a ticket by number.
 The ability to ask for a ticket's price.
 The ability to println a ticket object as a String. An example string
would be "Number: 17, Price: 50.0".

b) Implement a class named WalkupTicket to represent a walk-up event


ticket. Walkup tickets are also constructed by number, and they have a
price of $50.00.

c) Implement a class named AdvanceTicket to represent tickets


purchased in advance. An advance ticket is constructed with a ticket
number and also how many days in advance the ticket was purchased.
Advance tickets purchased 10 or more days before the event cost $30,
and advance tickets purchased fewer than 10 days before the event cost
$40.

d) Implement a class named StudentAdvanceTicket to represent tickets


purchased in advance by students. A student advance ticket is
constructed with a ticket number and also how many days in advance
the ticket was purchased. Student advance tickets purchased 10 or more
days before the event cost $15, and advance tickets purchased fewer
than 10 days before the event cost $20 (half of a normal advance ticket).
When a student advance ticket is printed, the string should mention that
the student must show their student ID. For example, "Number: 17, Price:
15.0 (ID required)".

3.

Alibata Business and Technology Solutions Inc. Page 8

You might also like