can someone plz explain why each option is valid or invalid..i have no clue of waht is going on in these questions? all i can see are they are being multiplied?.. i do not understand these attributes and instances and cannot find any helpful resources either? i know in the question below does not ask for attributes or instances but if u could in brief about the classes?
49. Given the following:-
class H {
public int z;
public String x;
}
class G extends H {
public double w;
public double y( ) { }
}
Which one of the following sets of statement is not valid:-
a) H r = new H( ); r.z = 99;
b) G r = new G( ); r.z = 9;
c) G r = new G( ); double d = r.y( );
d) G r = new G( ); r.w = 9.9;
e) H r = new H( ); r.x = 9.9;

Given the following Java code:-
public class D{
private int a;
private String c;
}
Which one of the following sets of statements is valid
a) D p = new D( ); p.a = p.a + 3;
b) D p.a = new D( ) + 3;
c) D p = new( ); a = a + 3;
d) D p = new D( ); a.p = a.p + 3;
e) D p.a = new(int) + 3

Dani AI

Generated

A few focused clarifications for , building on ’s replies.

A class is a blueprint; an instance (object) is what you get when you use new. An attribute (field) is data stored on each instance; a method is behavior that may return a value. Public fields are accessible from other classes; private fields are only accessible from inside the same class (inside the { ... } that defines that class).

For the first question: the only real type-error is assigning a numeric literal to a String field — that does not compile. Inheritance means a subclass instance can use public fields declared in its superclass. A method call like r.y() is fine if the method actually returns the declared type and its body contains a proper return statement; if the method body is empty or missing a return, the code will not compile even if the call matches the declared return type.

For the second question: private fields cannot be read or written from outside the class, so none of the provided statements are valid when executed from another class. The correct way to let other code change or read those fields is to provide methods inside the class (getters/setters or specific mutators). Example pattern:

class Counter {
  private int value;
  public void add(int n) { value += n; }
  public int getValue() { return value; }
}

Counter c = new Counter();
c.add(3);
int v = c.getValue();

Quick exam checklist: verify type compatibility (String vs numeric, int vs double), check that a method actually returns the declared type, and check access modifiers (private vs public). If something looks syntactically odd (bad new usage, odd variable names), treat it as an invalid statement as well.

Recommended Answers

All 6 Replies

no, YOU tell US what you think, and we may tell you if you're right.

1st one is 'e'? and 2nd is 'a'? if i am right for the frist and second ones.. is it coz its a string for the 1st one (X is a string) so r.X wont make sense??

second one i dont knw and that is the only one.. i can see which seems valid.. but i am not sure about why the rest wont work? please help..

thx

First question you got right. Indeed you cannot assign a numeric value to a String like that.
Second you got wrong. Look at the access modifier of the field. It's private, that means you can't assign to it from outside the class itself.
In fact, none of the statements given as possible answers there are correct.

what do u mean when you say outside the class?.. Is it before you close the "}" bracket, cause it is still inside the class right?

read up on what private means and you should understand.

ok.. thx.. one lat quetion.. what are are the instances and attribute in this? or an example plz..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.