02 Prelim CP2 (continuation) Flashcards

1
Q

The (1)_____ keyword can be used to allow
declaration of static variables and
methods that belong to the (2)_____ instead of a (3)______ instance (such as the main method):

A
  1. static
  2. Class
  3. Specific
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the static variable in this code:

public class StaticExample {

 static int staticNumber = 10;

 static void staticMethod() {
     System.out.println("Static number is: " + staticNumber)
 }

 public static void main (String[] args) {

 StaticExample.staticMethod();

 System.out.print(staticNumber):

 } }
A

staticNumber

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the method in this code:

public class StaticExample {

 static int staticNumber = 10;

 static void staticMethod() {
     System.out.println("Static number is: " + staticNumber)
 }

 public static void main (String[] args) {

 StaticExample.staticMethod();

 System.out.print(staticNumber):
 }

}

A

staticMethod()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the output of this code:

public class StaticExample {

 static int staticNumber = 10;

 static void staticMethod() {
     System.out.println("Static number is: " + staticNumber)
 }

 public static void main (String[] args) {
 StaticExample.staticMethod();

 System.out.print(staticNumber):
 }

}

A

Output:

Static number is: 10
10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

(1)_______ refers to the process of creating an actual object (or instance) from a class. In object-oriented programming, a class is like a blueprint, and
(2)______ the class means creating an object based on that blueprint.

A
  1. Instantiation
  2. Instantiating
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

instantiating the class means….

A

creating an object based on that blueprint.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

(1)________Instantiation allows you to reuse the same class multiple times to create different objects with potentially different states. This reduces code (2)_______.

A
  1. Reusability
  2. Redundancy
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output:

public class InstanceExample {

// Instance variable (not static)
 int instanceNumber = 10;

 // Instance method (not static)
 static void InstanceMethod() {
     System.out.println("Instance number is: " + instanceNumber)
 }

 public static void main (String[] args) {

 // Create an object (instatiate the class)
 instanceExample example = new instanceExample();

 // call the instance method using the object 
 example.instanceMethod():
 } }
A

Output:

Instance number is: 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the output:

public class sample {

 static int count = 0 ; // shared by all

 public void increment () {
      count ++; // increment shared variable }
 
 public static void main (String[] args) {

      sample A = new Sample(); // first instance
      sample B = new Sample(); // second instance 

 A.increment() // count becomes 1
 System.out.println("A.count");
 System.out.println("Object A: " + A.count ); // prints 1

 B.increment() // count becomes 2
 System.out.println("Object B: " + B.count ); //prints 2
 System.out.println("Object A: " + A.count ); // prints 2

 } }
A

A.count
Object A: 1
Object B: 2
Object A: 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

For example, we could define a method named sum that returns the sum of its two (2) parameters:

Syntax:
static int sum(int val1, int val2) {
return val1 + val 2 ;
}

A

Return Type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Return Method (Return type)
What is the output:

class Myclass {
static int sum(int val1, int val2) {
return val1 + val2;
}

 public static void main(String[ ] args) {
 int x = sum(2,5);
 System.out.println(x);

 } }
A

Output: 7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What keyword should you use if you do not want your method to return anything?

A

Void

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Return Method Outputs:

// Sample 1
static int returnFive()
return 5;

// Sample 2 (has a parameter) {
static void sayHelloTo(String name)
System.out.println(“Hello”+ name)
}

Use john

// Sample 3
static void sayHello() {
system.out.println(“Hello world!”);
}

A

S1: 5
S2: Hello John
S3: Hello World!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Fill in the blanks to declare an integer and pass it as a parameter to the test() method:

class MyClass {
public static void main(String [] args) {
____ x = 10;
test(__);
}

 static void test(int x) {
      System.out.println(x);
 } }
A

int x = 10;
test(x) ;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

______ are basically variables within a class:

A

Attributes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Access Modifiers

A
  1. default
  2. public
  3. protected
  4. private
17
Q

Access Modifier

public static void main(Stringl ] args)

______ is an access modifier, it is used to set the levels of access.

18
Q

______ a variable or method declared with no access control modifier is available to any other class in the same package

19
Q

□ _______ - accessible from any other class.

20
Q

□ _______ - provides the same access as the default access modifier, with the addition that subclasses can access protected methods and variables of the superclass.

21
Q

□ _______ - accessible only within the declared class itself.

22
Q

______ and ______ are used to effectively
protect your data when creating classes.

A

Getters
Setters

23
Q

> The _____ _____ returns the value of the
attribute.

A

Getter method

24
Q

> The _____ ____ takes a parameter and
assigns it to the attribute.

A

Setter method

25
Q

Getters start with (1)______, followed by the variable name, with the first letter of the variable name (2)_____.

Setters start with (3)___, followed by the variable name, with the first letter of the variable name (2)_______.

A
  1. get
  2. Capitalized
  3. set