Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Suppose the following methods are defined in some class: public static int someMethod(int x) { return x + some0therMethod(); } public int some0therMethod(int y) {

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed Suppose the following methods are defined in some class: public static int someMethod(int x) \{ return x + some0therMethod(); \} public int some0therMethod(int y) \{ return y z; \} Assume that the rest of the surrounding class has been defined correctly, and that z is the name of a static field variable in that class. What, if anything, is wrong with this code? a. A non-static method is calling a static field variable, which is not allowed. b. A static method is calling a non-static method, which is not allowed. c. Nothing is visibly wrong with the code (other than uninformative method and variable names) d. A static method is calling a non-static field (instance) variable, which is not allowed. e. A non-static method is calling a static method, which is not allowed. Which of the following is true with respect to static variables. Choose all that apply. a. The memory allocation of a static variable is done only once, during the time of class loading. b. Static variables can only be accessed by code in static methods of their class. c. Static variables can be private or public. d. Static variables must be class level. Suppose the following methods are defined in some class: public int someMethod(int x ) \{ return x+some0therMethod(x); \} public static int some0therMethod(int y) \{ return y z; \} Assume that the rest of the surrounding class has been defined correctly, and that z is the name of a static field variable in that class. What, if anything, is wrong with this code? a. A static method is calling a non-static variable, which is not allowed. b. A non-static method is calling a static variable, which is not allowed. c. A static method is calling a non-static method, which is not allowed. d. Nothing is visibly wrong with the code (other than uninformative method and variable names) e. A non-static method is calling a static method, which is not allowed. Suppose the following methods are defined in some class: public int someMethod(int x ) \{ return x+ some0therMethod(); \} public static int some0therMethod(int y) \{ return yz; \} Assume that the rest of the surrounding class has been defined correctly, and that z is the name of a non-static field variable in that class. What, if anything, is wrong with this code? a. A non-static method is calling a static field variable, which is not allowed. b. A static method is calling a non-static field (instance) variable, which is not allowed. c. A static method is calling a non-static method, which is not allowed. d. Nothing is visibly wrong with the code (other than uninformative method and variable names) e. A non-static method is calling a static method, which is not allowed. Suppose the following method is defined in some class: public int someMethod(int x ) \{ return x+z; \} Assume that the rest of the surrounding class has been defined correctly, and that z is the name of a static field variable in that class. What, if anything, is wrong with this code? a. A static method is calling a non-static field (instance) variable, which is not allowed. b. Nothing is visibly wrong with the code (other than uninformative method and variable names) c. A static method is calling a non-static method, which is not allowed. d. A non-static method is calling a static field variable, which is not allowed. e. A non-static method is calling a static method, which is not allowed. Consider the following partial code for some Java class: public class Person \{ public static final int maxPeople =10; public Person() \{ /*... rest of class continues here... */ Assume the remainder of the class is defined correctly. What, if anything, is the biggest problem with this portion of code? a. The variable maxPeople should not be initialised until a constructor is called. b. The access modifier for maxPeople should be private, not public. c. There is nothing that will cause a syntax error in the code, however, convention dictates that the maxPeople variable name should be capitalised with underscores, rather than in camel case. d. There is nothing visibly wrong with the code, syntactically or otherwise. e. A field cannot be both static and final. Consider the following partial code for some Java class: public class Person \{ public static final int MAX_PEOPLE; public Person() \{ /*... rest of class continues here... */ Assume the remainder of the class is defined correctly. What, if anything, is the biggest problem with this portion of code? a. The access modifier for maxPeople should be private, not public. b. The variable MAX_PEOPLE should be initialised at the same time it is declared. c. There is nothing that will cause a syntax error in the code, however, convention dictates that the MAX_PEOPLE variable name should be written in camel case (i.e., maxPeople). d. A field cannot be both static and final. e. There is nothing visibly wrong with the code, syntactically or otherwise. Constant (final) variables in Java can be... a. Local variables, but not fields. b. Fields (static and non-static), but not local variables. c. Fields (static and non-static) or local variables. d. Static fields or local variables, but not instance variables (non-static fields). e. Instance variables (non-static fields) or local variables, but not static fields. Suppose you have a Java class, Student, which needs to keep track of an ID number for each student object that is created. This ID number needs to be different for each student, but once it has been assigned to a particular student, its value should not be changed. What is the best way of defining and initialising the ID variable? a. As a non-final instance variable, i.e., private int ID, whose value is set by the class constructor. b. As a constant instance variable, i.e., private final int ID, whose value is set by the class constructor. c. As a non-final class variable, i.e., private static int ID, whose value is set by the class constructor. d. As a constant class variable, i.e., public static final int ID =12345678; Consider the following code fragment: Room office = new Room(); Room classroom = new Room(); office. setArea(125.0); classroom. setArea(350.0); office = classroom; classroom. setArea(200.0); Which of the following will be true, after this code executes? (Assume all classes and methods have been defined elsewhere correctly.) a. classroom.getArea() will return 200.0 and office.getArea() will return 125.0. b. classroom.getArea() will return 350.0 and office.getArea() will return 125.0. c. If you think all of the above will be adversely affected, select this option d. classroom.getArea() will return 200.0 and office.getArea() will return 350.0. e. office.getArea() and classroom.getArea() will both return 200.0. f. toString g. The code will result in a syntax error

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Describe the seven standard parts of a letter.

Answered: 1 week ago

Question

Explain how to develop effective Internet-based messages.

Answered: 1 week ago

Question

Identify the advantages and disadvantages of written messages.

Answered: 1 week ago