This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

OCA/OCP Java SE8 Programmer Practice Tests

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started to read the above book that was bought and downloaded yesterday. Assuming this is the latest version, I would like to seek clarification on the following:

Chapter 1: Java Basics

1. D. An entry point in a Java application….Option A is missing the static modifier,…
(This contradicts with the choice in the following, which allows main() without static modifier?)

10. B. Notice in this question that main() is not a static method, therefore it can access both class and instance variables….

21. C. The code compiles and runs without issue, …. Finally, the variable tomorrow is static…
(Should be: Finally, the variable yesterday is static?)

39. D. The proper extension for a Java compiled bytecode file is .java, making Option D the correct answer.
(Should be Java Source file is .java?)

Thanks.
 
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please supply more details about the book, including authors. Also fuller details about the questions; those of us who don't have that book can't tell which is the correct answer otherwise. Sorry.
 
Krish Krishnan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book is "OCA/OCP Java SE8 Programmer Practice Tests" Authors: Scott Skelikoff and Jeanne Boyarsky.  
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the authors' names.
Don't know about any question. I would need to see the full question to be able to answer.
 
Krish Krishnan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: 1. Which of the following method signatures is a valid declaration of an entry
point in a Java application?
A. public void main(String[] args)
B. public static void main()
C. private static void start(String[] mydata)
D. public static final void main(String[] mydata)

Answer: 1. D. An entry point in a Java application consists of a main() method with a single String[] argument, return type of void, and modifiers public and static. The name of the variable in the input argument does not matter. Option A is missing the static modifier, Option B is missing the String[] argument, and Option C has the wrong access modifier and method name. Only Option D fulfills these requirements. Note that the modifier final is optional and may be added to an entry point method.
(This contradicts with the choice in the following, which allows main() without static modifier)

Question: 10. Given the following application, fill in the missing values in the table
starting from the top and going downward.
package competition;
public class Robot {
static String weight = "A lot";
/* default */ double ageMonths = 5, ageDays = 2;
private static boolean success = true;
public void main(String[] args) {
final String retries = "1";
// P1
}
}
Variable Type Number of Variables Accessible at P1
Class _______
Instance _______
Local _______
A. 2, 0, 1
B. 2, 2, 1
C. 1, 0, 1
D. 0, 2, 1

Answer: 10. B. Notice in this question that main() is not a static method, therefore it can access both class and instance variables. Since there are two class variables and two instance variables defined, Option B is the correct answer

Question: 21. What is the result of compiling and executing the following class?
1: public class Tolls {
2: private static int yesterday = 1;
3: int tomorrow = 10;
4: public static void main(String[] args) {
5: Tolls tolls = new Tolls();
6: int today=20, tomorrow = 40;
7: System.out.print(today + tolls.tomorrow +
tolls.yesterday);
8: }
9: }
A. The code does not compile due to line 6.
B. The code does not compile due to line 7.
C. 31
D. 61

Answer: 21. C. The code compiles and runs without issue, so Options A and B are incorrect. The question relies on your ability to understand variable scope. The variable today has local scope to the method in which it is executed. The variable tomorrow is re-declared in the method, but the reference used on line 7 is to the instance variable with a value of 10. Finally, the variable tomorrow is static. While using an instance reference to access a static variable is not recommended, it does not prevent the variable from being read. The result is line 7 evaluates and prints (20 + 10 + 1) = 31, making C the correct answer.
(Should be: Finally, the variable yesterday is static)

Question: 39. What is the proper filename extension for a Java source file?
A. .jav
B. .class
C. .source
D. .java

Answer: 39. D. The proper extension for a Java compiled bytecode file is .java, making Option D the correct answer.
(Should be Java Source file is .java?)





 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Krish Krishnan wrote:. . . . This contradicts with the choice in the following, which allows main() without static modifier?)

The two methods are independent, being in different questions. It is quite all right to declare a main() method as instance. All the second question requires is counting the different kinds of variables; they are all accessible at point P1.

. . . . Finally, the variable tomorrow is static . . . yesterday is static?

I think you are right there. Please look here and see whether that has been reported already.

. . . (Should be Java Source file is .java?)

You appear to be correct there too; please check the same link above.
 
Krish Krishnan
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for the clarifications.
I understand the main methods referred were independent (in 2 questions). Still the first indicates that the method declaration is not valid (Option A is missing the static modifier). However, the 2nd question is a (valid) example of main without the static modifier. This led to the confusion - is the static modifier required for the main to be valid?
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for delay.
This Java® Language Specification (=JLS) page tells you the requirements for starting an application off. Try adding final and you will see that doesn't make any difference.
If you try compiling code with and without the static keyword, you will find that all overloaded versions of main(...) are ordinary methods and they will compile.
 
knowledge is the difference between drudgery and strategic action -- tiny ad
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!