zuai-logo

What are the differences between accessor and mutator methods?

Accessor: Retrieves value of a private variable. Mutator: Modifies value of a private variable.

All Flashcards

What are the differences between accessor and mutator methods?
Accessor: Retrieves value of a private variable. Mutator: Modifies value of a private variable.
What are the differences between static and instance variables?
Static: Belongs to the class, shared by all objects. Instance: Belongs to each object, unique to each object.
What are the differences between global and local scope?
Global: Accessible throughout the program. Local: Accessible only within a specific block of code.
What does the following code output? public class Test { private int x = 5; public static void main(String[] args) { Test t = new Test(); System.out.println(t.x); } }
5
Identify the error in the following code: public class Example { private int value; public void Example(int v) { value = v; } }
The constructor has a return type. It should be 'public Example(int v)' without 'void'.
What does the following code output? public class Counter { private static int count = 0; public Counter() { count++; } public static int getCount() { return count; } public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); System.out.println(Counter.getCount()); } }
2
Identify the error in the following code: public class MyClass { private int x; public void setX(int val) { x = value; } }
The variable 'value' is not defined in the setX method. It should be 'x = val;'.
What does the following code output? public class Point { int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public static void main(String[] args) { Point p = new Point(2, 3); System.out.println(p.x + ", " + p.y); } }
2, 3
Identify the error in the following code: public class Circle { private double radius; public double getArea() { return 3.14 * radius; } }
radius is not initialized
What does the following code output? public class Test { public static void main(String[] args) { String str = "Hello"; modifyString(str); System.out.println(str); } public static void modifyString(String s) { s = s + " World"; } }
Hello
Identify the error in the following code: public class Example { public static void main(String[] args) { int num = 5; increment(num); System.out.println(num); } public void increment(int n) { n++; } }
The increment method is not static and is being called from a static method.
What does the following code output? public class MyClass { int x; public MyClass(int x) { this.x = x; } public static void main(String[] args) { MyClass obj1 = new MyClass(10); MyClass obj2 = obj1; obj2.x = 20; System.out.println(obj1.x); } }
20
Identify the error in the following code: public class Rectangle { private int length; private int width; public Rectangle(int l, int w) { length = l; width = w; } public int area() { return length * width; } public static void main(String[] args) { Rectangle rect = new Rectangle(5, 10); System.out.println(rect.Area()); } }
Method name should be area(), not Area().
How is classes applied in real-world scenarios?
Classes are used to model real-world entities, such as bank accounts, cars, or employees, with their associated data and behaviors.
How is accessor methods applied in real-world scenarios?
Accessor methods are used to safely retrieve information about an object without allowing direct modification of its internal state.
How is mutator methods applied in real-world scenarios?
Mutator methods are used to update the state of an object in a controlled manner, often with validation or side effects.
How is static variables applied in real-world scenarios?
Static variables can be used to store application-wide settings or counters that need to be shared across all instances of a class.
How is the 'this' keyword applied in real-world scenarios?
The 'this' keyword is used to disambiguate between instance variables and local variables with the same name, ensuring that the correct variable is being accessed or modified.
How is comments applied in real-world scenarios?
Comments are used to explain complex logic, document API usage, and provide context for other developers who may need to maintain or extend the code.