{ "@context": "https://schema.org", "@type": "Organization", "name": "Brainscape", "url": "https://www.brainscape.com/", "logo": "https://www.brainscape.com/pks/images/cms/public-views/shared/Brainscape-logo-c4e172b280b4616f7fda.svg", "sameAs": [ "https://www.facebook.com/Brainscape", "https://x.com/brainscape", "https://www.linkedin.com/company/brainscape", "https://www.instagram.com/brainscape/", "https://www.tiktok.com/@brainscapeu", "https://www.pinterest.com/brainscape/", "https://www.youtube.com/@BrainscapeNY" ], "contactPoint": { "@type": "ContactPoint", "telephone": "(929) 334-4005", "contactType": "customer service", "availableLanguage": ["English"] }, "founder": { "@type": "Person", "name": "Andrew Cohen" }, "description": "Brainscape’s spaced repetition system is proven to DOUBLE learning results! Find, make, and study flashcards online or in our mobile app. Serious learners only.", "address": { "@type": "PostalAddress", "streetAddress": "159 W 25th St, Ste 517", "addressLocality": "New York", "addressRegion": "NY", "postalCode": "10001", "addressCountry": "USA" } }

Chapter 1-3 Flashcards

(27 cards)

1
Q

How to create an object of a class named “Alternative” = “Instantiate an object of the Alternative class”

A

Alternative myAlternative = new Alternative();

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

How do you define a method in Java that doesn’t return a value?

A

public void methodname(){
//code for method
}

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

How do you call a method named printMessage() on an object myObject = “invoke the printMessage() method on an instance of the Alternative class”

A

myObject.printMessage();

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

how do you define a setter method in java for a string?

A

public void setCat(String cat){
this.cat = cat;
}

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

how do you define a getter method for the cat?

A

public String getCat(){
return cat;

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

how do you call a setter method setCat(“masha”) on an object myAlternative?

A

myAlternative.setCat(“Masha”);

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

how do you call a getter method getCat() on an object myAlternative and print result?

A

System.out.println(myAlternative.getCat());

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

declare a private int instance variable for storing the age of a person inside a class

A

private int age;

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

why would you write an if else statement to check value of a variable

A

when you want to execute different blocks of code that depends on whether condition is true or false. helpful for program to handle different scenarios dynamically

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

how does an if-else statement control program flow

A

if-else allows program to decide which block of code to run based on condition.

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

given a user’s input, write an if-else statement to determine whether the user is allowed to access a feature (based on age)

A

if(age>=18){

System.out. (access granted)
}else{
sys. (access denied)

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

when would you use else if

A

when you have more conditions to check to handle multiple outcomes (different grades or categories)

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

given user input, write if-else statement to print different message for each input

A

if (input.equals(“yes”)) {
System.out.println(“You chose yes.”);
} else {
System.out.println(“You chose no.”);
}

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

when might you use nested if-else statement

A

when you need to check another condition only if the first is true. allows for complex-deciision making where conditions depend on eachother

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

write nested if-else to check if a number is positive, if positive, check if even or odd

A

if (number > 0) {
if (number % 2 == 0) { //no numbers left
System.out.println(“The number is positive and even.”);
} else {
System.out.println(“The number is positive and odd.”);
}
} else {
System.out.println(“The number is negative or zero.”);
}

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

what is basic syntax for a for loop in java

A

for(initialization ; condition; increment/decrement){
//code to be executed for each iteration

17
Q

do-while loop syntax:

A

do{
//code to be executed atleast once
} white(condition);

18
Q

main difference between a for loop and while loop

A

for loop is used when you know how many times the loop should run in advance

while loop is when the number is not known, and you need to continue looping based on condition

19
Q

how would you use a for loop to print num from 1-5

A

for (int i = 1; i <= 5; i++) {
System.out.println(i);
}

20
Q

how would you use for loop to iterate over an array of numbers and print each val

A

write a for loop that iterates over an array of integers and prints each element:

int[] numbers = {10,20,30,40,50}
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}

21
Q

what is an array?

A

collection of elements (values) of the same type. stored in contiguous memory lovations. each element cant be accessed by its index, starting from 0

22
Q

how do you declare an array of integers with 5 elements in java

A

int[] numbers = new int[5];

23
Q

how do you declare and initialize an array with values at the same time?

A

int[] numbers = {10,20}

24
Q

how do you access an element in an array

A

you access using its index. array indices start at 0.
int firstElement = numbers[0];
System.out.println(firstElement);

25
how do you modify an element in an array
numbers[2] = 100; // changes 3rd element to 100
26
how to use for loop to iterate an array and print each element
for (int i = 0; i< numbers.length ; i++){ System.out.println(numbers[i]); // this loop prints each element of the numbers array, using index i to access each val
27
how do you declare a 2d array (array of ararys)
int[][] matrix = new int[3][3];