Chapter 4--arrays, Loops, Logic Flashcards
(19 cards)
How do you add elements to the array?
Integer[] booger=new Integer[2]; booger[0]=new Integer(2); booger[1]=new Integer(7);
Can you have different data types in am array?
No
What is the default value for integers?
Zero
Variable scope?
The part of a program where a variable exists and can be used
Create a basic if statement that prints a string if specific conditions exist.
if ( farts.length>1{
System.out.println(“yizznar”);
}
Code a basic if else statement
String jomama; int holesfilled; if (holesfilled=1){ System.out.println("mom banged!") }
What is a common mistake when programming loops?
Putting a semicolon at the end of the for statement. This should only be done if the loop needs no body.
Give the basic format for a loop, and code a short loop
for (initialization; test; increment){
statement;
}
int age;
for (age=0; age);
what does System.out.format do?
Prints with number formatting
Is else if at the same tab level as if?
Yes
What are the data types usable as the test variable for a switch?
byte, short, int, char, String class
Code a basic switch
int dicksize=6; switch (dicksize){ case 4: System.out.println("it\'s small"); break; case 12: System.out.println("it\'s big"); break; default: System.out.println("it\'s meh"); }
Code a switch where multiple values need to execute the same statement.
int dicksize=6; switch (dicksize){ case 1: case 2: case 3: case 4: System.out.println("it\'s small"); break; case 12: System.out.println("it\'s big"); break; default: System.out.println("it\'s meh"); }
Give the format of a ternary operator, and code an example.
test ? trueresult : falseresult;
if the test is true, the trueresult is returned. Otherwise, the falseresult is returned. e.g.,
int vern=oBama>rOmney ? truth : lies;
How do you create an array the shortcut way?
int[] butthead={1994, 2007, 2017};
Code a while loop
while dick<15{
dick=dick+1;
}
Code a do-while loop
int vern=7; do{ vern+=7; System.out.println(vern + " "); }while (vern<49);
What are the values in an array Initialized to? Numeric, Boolean, char, and objects
Zero, false, ‘\0’, and null
When you move around values in an array of objects, are you copying values, or reassigning references? Are there exceptions to this?
Reassigning references. String objects are the exception