Shared Flashcard Set

Details

Java 2
Exam 1
24
Computer Science
Undergraduate 2
01/29/2012

Additional Computer Science Flashcards

 


 

Cards

Term

How do you declare an array variable?

Definition

datatype [] arrayName;

 

(double [] myList)

Term
How would you declare and create an array in 1 step?
Definition

datatype [] arrayName = new datatype [arraySize]


((( e.g., double [] myList = new double [10] )))

Term
How do you declare, create AND initialize a 1-dimensional array in 1 step?
Definition

        double[] myList = {1.9, 2.9, 3.4, 3.5};

equivalent to:

       double[] myList = new double[4];

       myList[0] = 1.9;

       myList[1] = 2.9;

       myList[2] = 3.4;

       myList[3] = 3.5; 

Term
If you use the shorthand method, what could give you an error?
Definition

splitting it:

double [] myList;

myList = {1.9, 2.9, 3.4, 3.5 } 

Term

How do you copy an array? 

 

How would you do it using a loop?

Definition

int [] sourceArray = { 2, 3, 1, 5, 10 }


int [] targetArray = new int [sourceArray.length];



When copying an array, use the assignment statement: (=)


list2 = list1;

Term

Pass By Value


What's the difference between passing a primative variable type and a reference variable type (i.e., an array variable)

Definition

- When passing a primative type to a method (double, char, int, boolean), the variable's value is passed, and any changes to the value within the method do not change the value of the argument outside of the method.


- When passing a reference type to a method (array, interface, class), the variable's value is passed--which is a reference point to the argument outside of the method. As a result, any changes made to the array via the parameter array variables passed inside of the method do affect the argument array outside of the method.

Term
When should one use 2-D arrays?
Definition
When one needs to store data in a table-like structure
Term
How do you declare a 2 dimensional array?
Definition

dataType [] [] refVar;

 

((( e.g., double [] [] myList; )))

Term
How do you declare and create a 2 dimensional array in 1 step?
Definition

dataType [] [] refVar = new dataType [10] [10]

 

((( double [] [] myList = new double [10] [10] ))) 

 

 

 

 

 

Term
How could you declare, create and initialize a 2 dimensional array using shorthand?
Definition

double [] [] myList = {

                                 {1, 2, 3}, 

                                 {4, 5, 6},

                                 {7, 8, 9},

                                 {10, 11, 12},

                               };

Term
What is a method?
Definition
A collection of statements that are grouped together to perform a function.
Term
How do you define a method?

What are the different parts? 
Definition

public static int userHand (int num1, int num2)

       ^        ^        ^             ^          ^

       |         |          |              |

    modifier   return value type    method name         parameter list - formal parameters

Term
What's the difference between formal parameters, and actual parameters?
Definition

Formal Parameters = variables defined in the method header.


(((( int num1, int num2 ))))


Actual parameter = (argument) the value passed to the method parameters when a method is invoked.


((((( max (x, y); ))))

Term
What is a void method? Give an example...
Definition

A void method returns no value.

 

public static void printStars (int n) 

{

   for (int i = 0; i < n; i++)

   system.out.print("*");

}


//no return statement needed, or, "return: ___"

Term

What is wrong with this non-void method example?

public static int sign (int n)

   if (n > 0)

      system.out.println(n);

   else

      return 0;

}


Definition

A return statement is required for a nonvoid method.

 

To fix this problem, add a return in the "if" branch, or, after the "if-else" statement entirely

Term
What is "Pass by value" ?
Definition

When an actual parameter is passed to a method, its value is copied into the formal paramater.

 

In other words, the actual parameter(argument) is a copy of the formal parameter (parameters)

 

 


EXAMPLE:


[image]


Term
What is "Scope?"
Definition

The part of the program where the variable can be seen/referenced.

 

Example:

 

[image]

 

The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable. 

Term
What is "Method Overloading"?
Definition

defining two or more methods, within the same class, that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading


Example:


public static double piggyButt (double first, double second);

public static double piggyButt (double third, double fourth);

Term
What is a "variable"?
Definition
A location in memory where a data value can be stored and changed
Term
What does an assignment statement do?
Definition
It gives a value to a variable (=)
Term
What is the syntax of an assignment statement?
Definition
Variable = Expression;
Term

What's the difference between these operators:

 

-   /

-  %

Definition

The division operator (/) divides a type and returns a floating point type (float or double), or integral type.

 

(e.g.,   10 / 4   has a value of 2 )


The modulus operator (%) returns the remainder of an integer type


(e.g.,   11 % 4   has a value of 3 )

Supporting users have an ad free experience!