• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor, Scope, Final variables,

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi everybody,
I ahve 3 questions:
Q1) Is it necessary for all Java classes to have atleast one constructor ?
Q2) Variables declared inside a method as final retain their value between invocations of the method in which they are defined. True or False
Q3) In the following code after which line does the variable var go out of scope ?

void func() {
for (int i=0; i<10; i++)
{
double var= i*3.2;
for(int j=0; j<10; j++) {
String s= String.valueOf(var*j);
System.out.println(s);
}
}
}

a) Line number 7
b) Line number 8
c) Line number 9

 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1) Is it necessary for all Java classes to have atleast one constructor ?

If you mean that the programmer's HAS to place the constructor in every java class. Then the answer is no. But java always puts a constructor to a class implicitly if one is not provided by a class designer. This implicit constructor is called the default constructor which does not have any arguments and the modifier is that of the class. If the class is public then the modifier is also public and so on.

Q2) Variables declared inside a method as final retain their value between invocations of the method in which they are defined. True or False
false. This is just to confuse C / VB programmers who know that a static variable's value is retained between method invocations.
Q3) In the following code after which line does the variable var go out of scope ?

a) Line number 7
b) Line number 8
c) Line number 9

I think it is after line 8 or at line 9
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable var is out of scope AFTER the closing brace on line 9.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic