Question

With this class: public class myFunction { public int x = 7; public int y =...

With this class: public class myFunction { public int x = 7; public int y = 3; } 1. What are the class variables? 2. What are the instance variables? 3. What is the output from the following code: myFunction a = new myFunction(); myFunction b = new myFunction(); a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.println("a.y = " + a.y); System.out.println("b.y = " + b.y); System.out.println("a.x = " + a.x); System.out.println("b.x = " + b.x); System.out.println("myFunction.x = " + myFunction.x);

Homework Answers

Answer #1
1. What are the class variables?
class variable is any variable declared with the static modifies.
There are no class variables in the given code.

2. What are the instance variables?
Variables x and y are instance variable.

3. What is the output from the following code:
System.out.println("myFunction.x = " + myFunction.x);
This lines gives compile time error.
Because x is not a class variable to access directly with class name.
Output of the code other than the above line is
a.y = 5
b.y = 6
a.x = 1
b.x = 2
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Java public class Point {      protected int x;      protected int y;      public Point...
Java public class Point {      protected int x;      protected int y;      public Point (int x, int y) {           this.x = x;           this.y = y;      }      public int getX() {           return x;      }      public int getY() {           return y;      } } THE FOLLOWING 3 QUESTIONS REFER TO THE CODE FRAGMENT ABOVE     Based on the code above, the Point Class is immutable: (a) TRUE (b) FALSE Explain: ___________________________________________ Subclasses...
what output is produced by the following code and explain how it works. public class A...
what output is produced by the following code and explain how it works. public class A { int a = 1; int b = 2; public int getSum(int a, int b) {     this.a+=a;     this.b+=b;     return this.a + this.b; } } public class B extends A { int a = 3; int b = 4; public int getSum(int a, int b) {     this.b=a;     super.b=b+b;     return super.a+this.b; } } public class q2 { public static void...
What is the output of the following code segment? public class Exception { ... static int...
What is the output of the following code segment? public class Exception { ... static int divider(int x, int y) { try { return x/y; } catch (ArrayIndexOutOfBoundsException a) { System.out.print("A"); return 1; } } static int computer(int x, int y) { try { return divider(x,y); } catch (NullPointerException b) { System.out.print("B"); } return 2; } public static void main(String args[]){ try { int i = computer (100, 0); } catch (ArithmeticException c) { System.out.print("C"); } } } ABC A...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class...
Java question Consider the following: 1 class SnoopDogg { 2 int count; 3 } 4 class Test { 5 public static void main ( String [] args ) { 6 SnoopDogg myCount = new SnoopDogg (); 7 myCount.count = 0; 8 int times = 0; 9 increment( myCount, times ); 10 System.out.println( myCount.count ); 11 System.out.println( times ); 12 } 13 public static void increment (SnoopDogg sd, int times ) { 14 sd.count = sd.count + 1; 15 times =...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
JAVA What values are stored in variables a and b in the code below? public class...
JAVA What values are stored in variables a and b in the code below? public class StackQuestion { public static void main(String[] args) { Stack s = new Stack(); s.push(1); s.push(2); s.push(3); s.pop(); s.pop(); s.push(4); s.push(5); s.pop(); s.pop(); int a = s.pop(); s.push(6); int b = s.pop(); } } What numbers are stored in variable a and b when the code below executes? public class QueueQuestion { public static void main(String[] args) { Queue s = new Queue(); s.enqueue(1); s.enqueue(2);...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Add a second data member (another int) to the class ObjX. Then in main assign a...
Add a second data member (another int) to the class ObjX. Then in main assign a value to the new variable and print it. class ObjX { // No "static" keyword for either member. int i; void print () { System.out.println ("i=" + i); } } public class DynamicExample { public static void main (String[] argv) { // First create an instance, which allocates space from the heap. ObjX x = new ObjX (); // Now access members via the...