0% found this document useful (0 votes)
110 views5 pages

Exercise 03: XBCS1093 Java Programming

The document contains 15 questions about Java programming concepts and syntax. It asks about output of code snippets, class and constructor definitions, keywords like final and this, exception handling, and more. It also provides two short programming exercises - one to print numbers in ascending order, and another to print a times table for a given number.

Uploaded by

Yim Gian Song
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views5 pages

Exercise 03: XBCS1093 Java Programming

The document contains 15 questions about Java programming concepts and syntax. It asks about output of code snippets, class and constructor definitions, keywords like final and this, exception handling, and more. It also provides two short programming exercises - one to print numbers in ascending order, and another to print a times table for a given number.

Uploaded by

Yim Gian Song
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

XBCS1093 Java Programming

Exercise 03

1. What is the output of this program? Explain your answer.

$ javac main_class.java
Exception in thread "main" java.lang.Error: Unresolved compilation
problem:
Duplicate local variable x

Compilation error.
Two variables with the same name can’t be created in a class.

2. Write a valid declaration of an object of class Box.


Box obj = new Box();

3. What is the output of this program? Explain your answer.

10 5
Arrays in java are implemented as objects, they contain an attribute that is
length which contains the number of elements that can be stored in the array.
Hence a1.length gives 10 and a2.length give s 5.
XBCS1093 Java Programming

4. Define a class named Publication that has four instance variables:


i. title: The title of the publication. It is a String value. The title
must contain at least 10 characters to be accepted.
ii. publicationDate: an object of class Date that specifies the date
of publication. The year must be greater than 1900 for the date
to be accepted.
iii. numberOfAuthors: specifies the number of authors.
iv. authors: an array of authors. Each author is an Author objects.
v. count: a static attribute used to calculates the number of
created Publication objects. You must update count for each
newly created object.

The class Publication has one constructor with arguments (String title,
Author [] a, Date date) that sets the values of the four instance variables to
those passed as parameters.
The class Publication has second constructor with arguments (String title,
int nbr, Date date) that sets the values of instance variables to those passed
as parameters and create the array of authors with nbr authors.

Finally, it defines the toString() method that returns the string in the format
“author1, author2,… , [title], publicationDate”.
Implement the class Publication.
public abstract class Publication {

private String title;


private Author [] authors;
private Date publicationDate;
private int numberofAuthors;

public static int count = 0;

Publication (String title, Author [] a, Date d) {


setTitle(title);
authors = a;
publicationDate = d;
count++;
XBCS1093 Java Programming

numberofAuthors = a.length;
}

Publication (String title, Author a, Date d){


setTitle(title);
authors = new Author[1];
authors [0] = a;
publicationDate = d;
count++;
numberofAuthors = 1;
}

Publication (String title, int nbr, Date d) {


setTitle(title);
authors = new Author[nbr];
publicationDate = d;
numberofAuthors = nbr;
count++;
}

public void setPublicationDate(Date d){


if (d.getYear()>1900)
publicationDate = d;
}

public void setTitle(String title){


if (title.length()>=10)
this.title=title;
}

public void setAuthors(Author []a){


authors= a;
XBCS1093 Java Programming

public String getTitle(){


return title;
}

public Author[] getAuthors(){


return authors;
}

public Date getPublicationDate(){


return publicationDate;
}

public String toString(){

String authorsList = "";


for (int i=0;i<authors.length;i++)
authorsList = authorsList + authors[i]+ ", ";
return String.format("%s [%s], %s.", authorsList, getTitle(),
getPublicationDate());
}
}

5. What if the main ( ) method is declared as private? Explain your answer.


It compiles without any errors, but in runtime, it says main method is not
public.

6. What is the final keyword in Java?


A variable can't be changed because final variable once assigned a
value can never be changed.

7. How to prevent any method from overriding?


Declare the method as final.

8. Explain the overloaded method.


The same method name with different types of parameters.
The same method name with different number of parameters.
XBCS1093 Java Programming

9. In Java, a try block should immediately be followed by one or more catch


blocks.

10. In a class definition, the special method provided to be called to create an


instance of that class is known as a/an constructor.

11. What are checked exceptions? Give an example.


Checked exceptions are those with the Java compiler forces you to
catch
E.g. IOException

12. What is the difference between error and an exception?


Error indicates serious problem that a reasonable application should not
try to catch.
Exception indicates conditions that a reasonable application might try to
catch.

13. What are the different ways to handle exceptions?


Java exception handling is managed via five keywords: try, catch, throw,
throws, and finally.

14. How are this() and super() used with constructors?


this() is used to invoke to another constructor in the same class
super() is used to invoke the superclass's constructor.

15. Is String a primitive data type in Java? Explain your answer.


It is not, String is group of characters put together. It differs from
primitive data type in terms of its behaviour
Write a program which asks the user to enter 3 numbers and then prints them out in
ascending order.
Write a program that allows the user to enter 2 numbers ln and n, the program prints
out the m items in the times table for n. For example, if the users enters 4 and 5, the
output is
1 times 5 = 5
2 times 5 = 10
3 times 5 = 15
4 times 5 = 20

You might also like