Krish Krishnan

Greenhorn
+ Follow
since Feb 13, 2020
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Krish Krishnan

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?
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?)





The book is "OCA/OCP Java SE8 Programmer Practice Tests" Authors: Scott Skelikoff and Jeanne Boyarsky.  
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.