Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Package brain teaser

 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think the answer is to this question, and please test any theories with javac first. Note, *test any theory by attempting to compile any code idea you have.

You have two files in the same directory, one contains the code for class F1 and the other for class F2 as follows.

package f1;
public class F1{}

package f2;
public class F2 extends F1{}


1) Class F1 will compile without error
2) The code for F1 and F2 will compile without error
3)The code for F1 will compile but the code for F2 will not compile
4) Both files will have a compile time error
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both files will have compile error
 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is

3)The code for F1 will compile but the code for F2 will not compile

Because the code for F1 is another package and code F2 doesnot know where the code F2 is located.

So compile error.

It work fines if you use import statements in COde F2.

Like ....import f1.F1; or import f1.*;

This is as per my knowledge .....What do you say ranchers?
 
Marcus Green
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Niranjan, what error message did you receive when you attempted to compile the code?. And if anyone else has a suggestion about this question that includes an error message, please include the text of any error messages (otherwise I may think you are just guessing)
[ December 31, 2005: Message edited by: Marcus Green ]
 
author
Posts: 9031
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Marcus!

I like the idea of these "compiler assisted" quizzes!
 
Marcus Green
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bates & Sierra (and Green ) can teach but when it comes to learning javac is your very good friend. I get huge amounts of feedback on questions where people have not tried compiling the example, and this is after the users have passed a screen that has big letters on it saying the following

Have you compiled the code yourself?
If the question asks what will happen if you attempt to compile and run some code, please try it yourself. Almost all feedback I get tends to be from people who have not actually tried to compile the code and are just guessing at what will happen. I am very, very interested in all feedback, but please cut and past the actual code and try to compile it to find out what it actually does. Please include your email address in the body of your feedback if you would like feedback. I do read and appreciate feedback and

have updated questions in response to some items. 1 August 2005. Marcus

Every other day or so I sit down and go through the feedback and usually some of them include the phrase "and what happened when you compiled the code?". Sometimes I also include the phrase "thank you for your feedback, you are correct and I have updated the question as a result"...
[ December 31, 2005: Message edited by: Marcus Green ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Marcus Green:
What do you think the answer is to this question, and please test any theories with javac first. Note, *test any theory by attempting to compile any code idea you have.

You have two files in the same directory, one contains the code for class F1 and the other for class F2 as follows.

package f1;
public class F1{}

package f2;
public class F2 extends F1{}


1) Class F1 will compile without error
2) The code for F1 and F2 will compile without error
3)The code for F1 will compile but the code for F2 will not compile
4) Both files will have a compile time error



Answer is 1.
on compilation of f2 following error is given
F2.java:2: cannot resolve symbol
symbol : class F1
location: class f2.F2
public class F2 extends F1{}
^
1 error

regards
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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