• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Declaration question

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following set of member declarations, which of the following is true?


int a; // (1)
static int a; // (2)
int f( ) { return a; } // (3)
static int f( ) { return a; } // (4


Select 2 correct options
a Declarations (1) and (3) cannot occur in the same class definition.


b Declarations (2) and (4) cannot occur in the same class definition.
A static method can refer to a static field.

c Declarations (1) and (4) cannot occur in the same class definition.
because f() is static and a is not.

d Declarations (2) and (3) cannot occur in the same class definition.


e Declarations (1) and (2) cannot occur in the same class definition


One correct option is : c
Other correct option : e

WHAT ABOUT option d?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct option is b.
Since on 2 & 4, there is a duplication of declaration of variable a and method f() respectively.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A choice of D is incorrect because a non-static method can access a static member:
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic