• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructor, Scope, Final variables,

 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable var is out of scope AFTER the closing brace on line 9.
reply
    Bookmark Topic Watch Topic
  • New Topic