final (theoritical) Flashcards

1
Q

Method of printing array using Arrays class:

A

import java.util.Arrays;
……
System.out.println(Arrays.toString(array name) );

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

How would the output look like?
System.out.println( Arrays.toString(_______) );

A

[ whatever is inside of the array ]

(if it was numbers for example it would look like this:
[2000, 4000, 7000, 5000]
)

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

Two array lists cannot be == even if they have the same length and contents, unless

A

we unionized their addresses (ex:
int[] list1 = {1,2,3};
int[] list2 = {1,2,3};
list1 = list2;
)

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

what is the output?
boolean[] b = new boolean[10];
System.out.println( Arrays.toString(b) );

A

[false, false, false, false, false, false, false, false, false, false]

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

What is the code for sorting an array list (ex: int[] x) (in ascending order)?

A

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;
}
}

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

What is the code for reversing an array list (ex: int[] x)?

A

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–;
}

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

what is the code for generating random number (ex: between 1-6 inclusive)?

A

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

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

T/F: Package is a collection of classes grouped in a folder.

A

t

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

T/F: the compiler automatically creates a default constructor for every java class.

A

f

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

T/F: if you want to define a constant -a final value- you must use the keyword static.

A

f

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

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.

A

f, we must create the object first with keyword new.

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

T/F: In the same class you can call a non-static method inside a static method direcly.

A

f

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

T/F: In class definition, you can call a static variable in a non-static method directly without using class name and a dot.

A

t

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

T/F: when you define a constructor it is of type void.

A

f, there is no type for constructor.

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

T/F: Constructors and set methods have similiar tasks in changing the object state.

A

t

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

T/F: Math class consists of static methods, which means you must call an object of the class to use them.

A

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)

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

solve: Math.pow(3,2); =

A

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)

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

solve; ‘c’ - ‘a’ =

A

2
(remember; you dont have to memorize any unicodes just think logically the difference between letters c and a is 2.)

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

what does Integer.parseInt(str); do?

A

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

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

what does Character.isDigit( str.charAt(0) ); do?

A

it returns a boolean (true: if the given character is a digit, false; if not.)

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

code for search (for repeated number) in array;

A

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)

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

T/F: you can call a void method in a print statement.

A

false

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

T/F: calling a non-static method by class name.

A

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

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

static is used when

A

It is a keyword which is used to share the same variable or method of a given class.

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

T/F: intializing an object with a default constructor, but there is no default constructor for that class.

A

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.

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

T/F: String is a reference variable.

A

true

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

T/F: the method can be nested.

A

f, a method can’t be nested with another method

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

T/F: The signature of a method contains the name, parameter list, and it’s return type.

A

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

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

T/F: In Java, String is NOT a primitive data type.

A

true

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

T/F: a reference variable stores the address rather than the value.

A

true

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

what is the code for a guessing game?

A

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 );

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

In a flowchart, parallelograms are for _________, and rectangles are for __________.

A

input/output/print/read, calculations

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

T/F: every java program must have main method and it could have other methods.

A

true, pay attention the question said every program not every class.

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

T/F: Variable of type integers consists of 6 bytes.

A

false, consists of 4 bytes

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

long = ??? bytes

A

8 bytes

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

short = ??? bytes

A

2 bytes

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

double = ?? bytes

A

8 bytes

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

float = ??? bytes

A

4 bytes

39
Q

T/F: double and float are variables of floating point type.

A

true

40
Q

T/F: Array types and class types are reference variables.

A

true, it’s a fact just memorize it.

41
Q

T/F: in this statement (System.out.println(“hello”);), System.out is the method, println is the object, and “hello” is the parameter.

A

f, Println is the method and System.out is the object

42
Q

T/F: is it legal to method method implementation to be nested.

A

f, methods can’t be nested with other methods.

43
Q

T/F: Array has one public instance variable called length.

A

true, it’s a fact just memorize it.

44
Q

T/F: when myArray passed to a method, the ability of that method to do permenant changes on the array depends on the base type of array.

A

false, it doesn’t depend on the base type of the array. any base type array will undergo a permenant change via the method.

45
Q

T/F: we can use a static variable with this.

A

false, we cannot because the static variable is global to the entire class it is in.

46
Q

T/F: a static method can only call another static method (without calling it by object).

A

true, if the other method is not static then we can only call it by object.

47
Q

what are the valid/allowed types for x to be used in switch (x)?

A

for x to be char or string or integer type (that includes byte, short but NOT LONG) ONLY

48
Q

What is the incorrection in this code?
import java.lang.Math.*;
-
-
-
System.out.println( pow(3, 2) );

A

import STATIC java.lang.Math.*;

(if we put static, then now we can use objects of the class Math without write Math. first)

49
Q

T/F: Every class must have a default constructor.

A

false

50
Q

T/F: instant variables are shared by every object of the class.

A

false

51
Q

T/F: A class (static) variable, can only be accessed through an object of that class.

A

false

52
Q

T/F: In java, it is allowed for a method to return multiple values.

A

false

53
Q

T/F: for a method overload to be correct, the parameter variable type must be different or the parameter list should be sorted/organized differently.

A

true, the return type of the method has NOTHING TO DO WITH OVERLOADING.

54
Q

T/F: If a method was invoked in a default constructor, then that method is static in that class.

A

false, it is either static or non-static method in class.
(because non-static method (In the case of this question: default constructor) has the freedom to invoke/call either static or non-static methods. while static methods can only invoke/call other static methods.)

55
Q

What is the value of s?
String w = “weee”;
String s = w.substring(1, 3);

A

ee

(remember: the substring ys7b on the character at the index of the 2nd number in a substring command and only takes in the character before it in the string.)

56
Q

T/F: When you parse an integer value with Double.parseDouble() it will cause a runtime error.

A

f, but that int must be stored in a string.

57
Q

in Math.floor, you round to the

A

left
ex:
double num = Math.ceil (2.4); ///2.0
double num = Math.ceil(-2.4); //-3.0
double num = Math.ceil(2.0); //2.0

58
Q

in Math.ceil, you round to the

A

right
ex:
double num = Math.ceil (2.4); ///3.0
double num = Math.ceil(-2.4); //-2.0
double num = Math.ceil(2.0); //2.0

59
Q

what can you write at the beginning of a code that uses a lot of Math. predefined methods?

A

import static java.lang.Math.*;

60
Q

the variables in a method header are called _________

A

formal parameter

61
Q

the variables that are called out in main not method are the ________

A

actual parameter

62
Q

correct the following:
import java.util.*;
public class testMethod{
scanner console = new Scanner(System.in);
public static void main (String[] args){

A

if the scanner is written before/outside the main, we must add “static” to it.

import java.util.*;
public class testMethod{
static scanner console = new Scanner(System.in);
public static void main (String[] args){

63
Q

correct the following:
public static void methodx(int x){
if ( x > 0 )
return x + 5;
}

A

incorrection (2): incorrect method type, and missing return

public static int methodx(int x){
if ( x > 0 )
return x + 5;
return x;
}

64
Q

if there is a line under an attribute in a UML then that means that we need to intialize it as….
(ex: + numOfFemale: int is one of the attributes of class trainee. )

A

public static int numOfFemale;

65
Q

the default value that java will give a public char/string:

A

null

66
Q

To use an object from another class it must be in the same ________

A

folder

67
Q

public void setTime(int h , int m, int s)
we call this:

A

setter (Mutator)

68
Q

public int getSec(){
return sec;
}
we call this:

A

getter (Accessors)

69
Q

T/F: java conducts a defaults constuctor if my code doesn’t have a constructor.

A

t

70
Q

T/F: A constructor is always public.

A

t

71
Q

T/F: the name of the constructor can be different from the name of the class.

A

false, it’s always the same as class

72
Q

which kind of copy is this?
Clock c4 = new Clock ( c2 );

A

deep copy (composetion)

73
Q

which kind of copy is this?
Clock c5 = c2;

A

shallow copy (Agregation)

74
Q

is a technique of having more than one constructor with different parameter lists

A

Constructor Overloading in Java

75
Q

method signature:

A

It includes the method name and parameter list.

76
Q

a method header includes:

A

access specifier (public/private), return type (int/double/etc), method name, parameter list

77
Q

You are not allowed to use a switch code on ________ or _________ variable.

A

boolean, double

78
Q

rewrite this code using the condition operator:
If( result == answer )
System.out.println( “ Correct “ ) ;
else
System.out.println( “ Error “ ) ;

A

System.out.println( result == answer ? “Correct” : “Error” ) ;

79
Q

T or F: A java program can involve any number of classes.

A

t

80
Q

the items inside paranthesises are called ______ and provide the info needed by methods.

A

arguments

81
Q

A ______ is something that can store data.

A

variable

82
Q

An instruction to the computer is called a ______ it ends with a semicolon.

A

statement;

83
Q

What are the two known programming approaches?

A

What are the two known programming approaches?

84
Q

T or F: the Object-Oriented programming is also known as modular programming.

A

F, the structured programming is also known as…

85
Q

______ are errors that are not detected during compilation or while running, but which cause the program to produce incorrect results

A

logic errors

86
Q

T or F: Algorithms usually are expressed in english and pseudocode.

A

true

87
Q

T/F: An algorithm describes the means of performing an action.

A

t

88
Q

What are the three primary design principles in OOP?

A

encapsulation, polymorphism, inheritance.

89
Q

T or F: programs are not usually created entirely from scratch.

A

true.

90
Q

T/F: Pascal, C, and Fortran are examples of programming model languages (structured).

A

T

91
Q

T/F: just because an array is String[] doesn’t mean we can use string type methods on it (like for example: ArrayString.toUpperCase() or ArrayString.length())

A

true

92
Q

T/F: When a method is invoked in a stand-alone statement, it is most likely of type void

A

true.

93
Q

T/F: When an attribute of a class is declared as static there is only one copy of it and it is shared by all instances of that class

A

true