professor-curious-logo
professor-curious-logo
  1. AP Computer Science A
FlashcardFlashcard
Study GuideStudy GuideQuestion BankQuestion BankGlossaryGlossary

What is the purpose of a constructor?

To initialize the instance variables of a class when an object is created.

Flip to see [answer/question]
Flip to see [answer/question]
Revise later
SpaceTo flip
If confident

All Flashcards

What is the purpose of a constructor?

To initialize the instance variables of a class when an object is created.

Why are instance variables often declared as private?

To encapsulate the data and control access to it from outside the class.

Explain the concept of pass-by-value.

When a primitive type is passed to a method, a copy of the value is passed. Changes to the parameter inside the method do not affect the original variable.

Explain how objects are passed as parameters.

A copy of the reference to the object is passed. Mutating the object inside the method will affect the original object.

What is the significance of the 'static' keyword?

It indicates that a variable or method belongs to the class itself, rather than to any specific instance of the class.

What are accessor methods used for?

Accessor methods are used to retrieve the values of private instance variables from outside the class.

What are mutator methods used for?

Mutator methods are used to modify the values of private instance variables from outside the class.

What is the purpose of documentation with comments?

To explain what your code does to document it properly.

What are the ethical and social implications of computing systems?

Privacy, security, inequality, censorship, and dependence.

What is the purpose of the 'this' keyword?

To access the fields and methods of the current object from within the object's own methods.

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().

What is the definition of a class?

A blueprint for objects, containing variables and methods.

What is an instance variable?

A variable that stores data within a class.

What is a constructor?

A special method to create and initialize objects of a class.

What is an accessor method?

A method (getter) used to retrieve the value of a private variable.

What is a mutator method?

A method (setter) used to modify the value of an instance variable.

What is a static variable?

A variable that works across all objects of a class, accessible without creating an object.

What is a static method?

A method that works across all objects of a class, accessible without creating an object.

What is scope in programming?

The visibility and accessibility of variables, functions, and other elements of a program.

What is global scope?

Visibility and accessibility of an element throughout the entire program.

What is local scope?

Visibility and accessibility of an element within a specific block of code.

What does the 'this' keyword refer to?

The current instance of an object.

What is a comment in Java?

A piece of text that the computer will not recognize as code and will not try to execute.