• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

questions about bonus exam practice (IOException/encapsulation/for loop)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
here some questions about the first bonus test:
question 36:

What is the output of the following program?
1: public class Supper {
2: public static void eat() throws IOException {
3: try {
4: System.out.print("1");
5: throw new IOException();
6: }catch(IOException e) {
7: System.out.println("2");
8: throw e;
9: }finally {
10: System.out.println("3");
11: }
12: }
13: public static void main(String [] args) {
14: eat();
15: System.out.println("4");
16: }
17: }

the code does not compile, but for me it is because java.io.* is not imported (write throws IOExcepiton doesnot compile)
in the test, they say: Because main() neither handles nor declares the IOException...
which is also true

question 39:

Which of the following are true of the following code? (Choose all that apply)
1: public class Giraffe {
2: public int height;
3: public int getHeight() {
4: return height;
5: }
6: public void setHeight(int height) {
7: this.height = height;
8: }
9: }


A. This class currently exhibits data encapsulation.

B. This class currently exhibits immutability.

C. Making line 2 private would exhibit data encapsulation.

D. Making line 2 private would exhibit immutability.

E. Making line 2 private and removing lines 6–8 would exhibit data encapsulation.

F. Making line 2 private and removing lines 6–8 would exhibit immutability

I choose C but the answer is:
C, E, F.
Data encapsulation requires private instance variables. Although setters are allowed in an encapsulated class, they are not required. Immutability requires private instance variables and no setters.

I am really confused about this one because I learned that a good encapsulation is:
- Declare the variables of a class as private.
- Provide public setter and getter methods to modify and view the variables values.

and immutability: remove setters and use constructors

question 46:

Q. 46 Which of the following are true statements? (Choose all that apply)

A. do while loops can execute the loop body exactly zero times.

B. do while loops contain an increment clause.

C. for loops can execute the loop body exactly zero times.

D. for loops contain an increment clause.

E. while loops can execute the loop body exactly zero times.

F. while loops contain an increment clause.

I choose C and E
but it is C, D and E
I don t understand why D, it can also be a decrement or another kind of update statement, no?

thank you for your help,
I have the exam in a week and I am really confused about those 3 questions.

Ana
 
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding your first question:

An IOException is a checked exception, which means it MUST be handled (with a try/catch block) or declared. The code does so for the first IO exception, which is nicely wrapped within a try/catch. However, in the catch block the IOException e is thrown again. This one is not handled, nor declared, hence the compilation error.
 
Brecht Geeraerts
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding question two:

Encapsulation is all about protecting your class from unwanted access. A field with a public access modifier (such as height) can per definition be accessed from everywhere. Making it private means that it can only be accessed (and potentially modified) from within Giraffe class itself, e.g. by a getter and/or setter method. If there are no such methods for a private field, there is no way to actually change the height.

Immutability means that an object, once it is instantiated, can no longer be changed (such as a String). By removing the setter for private int height;, you get an example of immutability. That is why E and F are also correct.

On the actual exam, the number of correct solutions is indicated, so you will know in advance how many boxes you need to tick.
 
Brecht Geeraerts
Rancher
Posts: 261
12
IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Regarding question 3:

You are right, it doesn't necessarily mean increment. It is better to consider it as an update statement:

All these are legal options:
 
Marshal
Posts: 80639
472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AP: please always tell us where such questions come from.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic