final (theoritical) Flashcards
Method of printing array using Arrays class:
import java.util.Arrays;
……
System.out.println(Arrays.toString(array name) );
How would the output look like?
System.out.println( Arrays.toString(_______) );
[ whatever is inside of the array ]
(if it was numbers for example it would look like this:
[2000, 4000, 7000, 5000]
)
Two array lists cannot be == even if they have the same length and contents, unless
we unionized their addresses (ex:
int[] list1 = {1,2,3};
int[] list2 = {1,2,3};
list1 = list2;
)
what is the output?
boolean[] b = new boolean[10];
System.out.println( Arrays.toString(b) );
[false, false, false, false, false, false, false, false, false, false]
What is the code for sorting an array list (ex: int[] x) (in ascending order)?
for( int i = 0 ; i < x.length - 1 ; i++ ){
for ( int j =i+1 ; j<x.length ; j++ )
if ( x[j] > x[i] ){
int temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
What is the code for reversing an array list (ex: int[] x)?
int temp;
int i = 0;
int j = x.length - 1;
while (i < j){
temp = x[i];
x[i] = x[j];
x[j] = temp;
i++;
j–;
}
what is the code for generating random number (ex: between 1-6 inclusive)?
(int) ( Math.random() * 6 ) + 1 );
(Don’t forget: the number you multiply to is the difference of the range +1.
and the number that you add (if the range was inclusive) is the 1st number.)
T/F: Package is a collection of classes grouped in a folder.
t
T/F: the compiler automatically creates a default constructor for every java class.
f
T/F: if you want to define a constant -a final value- you must use the keyword static.
f
T/F: Assume we define and declare an object using (ClassPet myPet; myPet.set(“cat”, “brown”, “10”);) it will change myPet’s attributes to these values.
f, we must create the object first with keyword new.
T/F: In the same class you can call a non-static method inside a static method direcly.
f
T/F: In class definition, you can call a static variable in a non-static method directly without using class name and a dot.
t
T/F: when you define a constructor it is of type void.
f, there is no type for constructor.
T/F: Constructors and set methods have similiar tasks in changing the object state.
t
T/F: Math class consists of static methods, which means you must call an object of the class to use them.
f, if you used import java.util.Math;
you can just write pow(__) without the Math. first (as in you don’t need to call the object of class to use them.. not nessecarily every time anyways)
solve: Math.pow(3,2); =
9.0
(so remember that Math.pow returns a double by nature, and we have to cast it.
just like in Math.random() where we always use the casting (int) with it)
solve; ‘c’ - ‘a’ =
2
(remember; you dont have to memorize any unicodes just think logically the difference between letters c and a is 2.)
what does Integer.parseInt(str); do?
returns the integer part of the string.
(ex: Integer.parseInt(“He was 40.”)
gives us
40.
if we put it in a printing statement ofc.)
what does Character.isDigit( str.charAt(0) ); do?
it returns a boolean (true: if the given character is a digit, false; if not.)
code for search (for repeated number) in array;
assume an array called int x[] has been decleared intialized and everthing..
int count = 0;
_
for(int i = 0; i < x.length ; i++ ){
for(int j = i+1; j < x.length ; j++)
if ( x[i] == x[j] )
_
count++;
if ( count == 0 )
return true;
else
return false;
(the main idea is in bold)
T/F: you can call a void method in a print statement.
false
T/F: calling a non-static method by class name.
f, this is an error only static method can call with class name directly.
(for ex: i called math.random in my method (which is non static), this is incorrect. it must be static since im using object random from static class math.)
static is used when
It is a keyword which is used to share the same variable or method of a given class.
T/F: intializing an object with a default constructor, but there is no default constructor for that class.
f, this is an error and it’s only okay to do if there were no constructors in the class not of you have a parameterized constructor without any default constructor, then it is wrong.
T/F: String is a reference variable.
true
T/F: the method can be nested.
f, a method can’t be nested with another method
T/F: The signature of a method contains the name, parameter list, and it’s return type.
f, the return type is not always included in the signature.
(ex: public void setName(String Name){
in bold is the signature of the method
as we can see, not every method has a return type. some only change/alter the attributes, etc.)
T/F: In Java, String is NOT a primitive data type.
true
T/F: a reference variable stores the address rather than the value.
true
what is the code for a guessing game?
int secret = (int) ((Math.random() * 100) + 1);
int count = 0;
System.out.println(“Enter a number (1 to 100):”);
do{
int guess = input.nextInt();
if ( guess > secret ){
System.out.println(“too big, try again!”);
count++;
}
else{
System.out.println(“too small, try again!”);
count++;
}
if ( guess == secret ){
System.out.println(“You win!”);
System.out.println(“Your number of trials: “ + count);
break;
}
}while( count < 10 );
In a flowchart, parallelograms are for _________, and rectangles are for __________.
input/output/print/read, calculations
T/F: every java program must have main method and it could have other methods.
true, pay attention the question said every program not every class.
T/F: Variable of type integers consists of 6 bytes.
false, consists of 4 bytes
long = ??? bytes
8 bytes
short = ??? bytes
2 bytes
double = ?? bytes
8 bytes