Shared Flashcard Set

Details

Java Basic Questions CH1-4
Java Basic Questions CH1-4
80
Computer Science
Not Applicable
11/02/2005

Additional Computer Science Flashcards

 


 

Cards

Term
Which keyword is not implmeneted in java because its harmful?
a. abstract
b. goto
c. transient
Definition
b. goto
Term
Instance of using const, for a constant which key word do we use?
a.static
b.final
c.private
Definition
b.final
Term
Why is this illegal ?
public int break(int b) { }
public int switch(int b) {}
Definition
break and switch are reserved key words
Term
Why is this illegal ?
public int break(int b) { }
public int switch(int b) {}
Definition
break and switch are reserved key words
Term
A char is a unicode char.. how many bits.


8,16,32,64?
Definition
16 bit unsigned.
Term
a double is a 64 bit flopating point
is it signed or unsigned?
Definition
SIGNED
Term
A flat is a ___ bit floating point number
Definition
32
Term
a long is a _____ bit ______ integer
Definition
64 signed
Term
an int is a a 32 bit SIGNED or UNSIGNED ?
Definition
SIGNED
Term
Both short and byte are SIGNED integers.
How many bit does a short have?
Definition
16
Term
Both short and byte are SIGNED integers.
How many bit does a bye have?
Definition
8
Term
From broadest to the narrowest which is the correct order?
double float, long, int, byte, short
double, float, int, long, byte,short
double, long, flat, short, int , byte
double, float, long,int, short, byte
Definition
double, float, long,int, short, byte
Term
Float f = 23.23568 fails, why?
Definition
Float f = 23.23568F (needs to be attached) because a FP literal
is deinfed a s a double (64 bits), not with 32 bits
Term
Why is this illegal?
int x = 1; if (x) { }
Definition
Because x is not "true" or "false"
Term
Char literals can also be represented as an integer, as long as it’s less than
1. 65536
2. 65537
3. 65538
Definition
1. 65536
Term
This deinfes what kind of character?
char c = ‘\”’;
1. double quote
2. single quote
Definition
1. double quote
Term
Is this legal?
char b = 982;
Definition
Yes because its an int literal
Term
Is this legal?
char c = 70000;
Definition
No, out of range, less than 65536, to make legal use cast
char c = (char) 70000
Term
Is this legal?
char c = -29
Definition
No , possible of loss of precision, need a cast, char is unsigned.
Term
Delcare an array Two ways: before or after name
a) int [ ] key;
b) int key [ ] ;
which is recommended (a) or (b)?
Definition
a
Term
Is this legal?
int[5] scores;
Definition
No, never legal to include the size of array in declaration
Term
Declare, construct, and assign this in one line.
TEST SCORES array of ints size of 4.
Definition
int [] testScores = new int[4];
Term
Is this legal?
Definition
No because arrays must always be given a size at the time they are constructed.
Term
Multidimensional Arrays - are these legal?
1. int [ ] [ ] ratings =
new int [3] [ ];
2. scores [0] = new int [4];
3. scores [1] = new int [6];
4. scores [2] = new int [1];
Definition
yes
Term
How do you intilize array of dots
in one line as a list.
Definition
int[] dots = {3,6,x,8};
Term
Is this legal?
Dog puppy = new Dog(“Frodo”);
Dogs[] myDogs = {puppy, new Dog (“Clover”), new Dog(“Aiko”)};
Definition
Yes
Term
int [ ] [ ] scores = { {5,2,4,7}, {9,2}, {3,4} };
scores [0] // holds this array {5,2,4,7}
scores [2] //holds this array {3,4}
scores[2][1] //Accesses which value?
Definition
4 [{3,4}]
Term
Legal?
int [] scores;
scores = new int[] {4,7,2};
Definition
YES
/used for just in time array
Term
scores = new int[3] {4,7,2} //legal?
Definition
scores = new int[3] {4,7,2} //legal? NO – do not put size.
Term
An array has a ____ relationship.
class Car{}, class Honda extends Car{}
i.e. Car [ ] my Cars = { new Honda() };
a. IS-A
b. HAS-A
Definition
IS-A
Term
int[] splats;
int [] dats = new int[4];
char[] letters = new char[5];
splats = dats; // legal?
Definition
Ok, dats refer to an int array
Term
int[] splats;
int [] dats = new int[4];
char[] letters = new char[5];

splats = letters //legal?
Definition
NO, letters refer to a char array
Term
• int[ ] [ ] books new int [3] [];
• int [ ] numbers = new int[6];
• int aNumber = 7;
• books[0] = aNumber // Compile?
Definition
No , expect an int array instead of int
Term
• int[ ] [ ] books new int [3] [];
• int [ ] numbers = new int[6];
• int aNumber = 7;
• books[0] = numbers //
Definition
Compile? Yes, numbers is an int array.
Term
default value for char is

a.‘\u0000’
b. 0
Definition
a.‘\u0000’
Term
When an array of objects is instantiated, objects within the array are instantiated automatically.
T or f?
Definition
F
Term
• When an array of primitives is instantiated, all elements get their default values
T or F
Definition
T
Term
• Just as with array elements, instance variables are always initialized with a default value.
T or F
Definition
T
Term
• Local/automatic/method variables are always given a default value. If you attempt to use one before initializing it, it will compile but Instance Variables are T or F
Definition
False
Term
//Local objects and arrays
Date d ;
if (d == null){
System.out.println(“date is null”);
}

Why will this crash?
Definition
//Will crash because you have to explicitly set d to null. A null references Is not the same as an uninitalized reference!
Term
• The first command-line argument is the first element in the main String array parameter.
%java TestMain Hello
//First arg is ____
Definition
Hello
Term
• If no arguments are passed to main, the length of the main String array parameter will be ____
Definition
zero
Term
HOw many public class per source code.

1. 0
2. 1
3. 2
Definition
1
Term
These classes are default, can this compile and why?

i.e. package cert; package exam.stuff
___class Bev{ } import cert.Bev;
____class Tea extend Bev { //can’
Definition
No because thtey are both in DIFFERENT packages. We can make one of the classes public to make it compile
Term
strictfp can modify which of the following

class
method
variable
Definition
class AND method ONLY
Term
Is this legal abstract strictfp class Blah?
Definition
Yes because strictfp applies to classes or methods.
Term
Is this legal?
final stricttfp class
Definition
Yes because strictfp applies to classes or methods
Term
public final class ? Is this legal?
Definition
Yes because of inheritance
Term
Is this legal?
final abstract class
Definition
NO, a class can never be FINAL AND ABSTRACT. contradiction abstract(has to be extended), final can’t
Term
Final class :
1.can’t be subclassed,
2.no class can extend a final class.

which is true?
Definition
both statements are
Term
Abstract class MKUST BE

1. Instantiated
2. Extended (sublcasses)
3. BOTH
Definition
2. Absturact classes can ONLY be extended NEVER instantiated
Term
Will this compile?

abstract class Car{
private double price;
public abstract void goFast();
}
Definition
YES
Term
class Honda extends Car { implement methods here }

Which is the parent abstract class?
Definition
Car
Term
Do rules of overriding apply for private methods
i.e. private void jump()
public void jump()
Definition
No it does not
Term
Default Members (Package restrictions)
- can be accessed only if the class accessing the member belongs to the ____ package.
a. different
b. SAME
Definition
SAME
Term
package cert;
public class Parent {
int x = 9 ;//default
}

package different;
class Child extends Parent{
public void testIt(){
System.out.println(x); // cam we ise this “x”.
}
}
Definition
NO
Term
package cert;
public class Parent {
int x = 9 ;//default
}


package cert;
class Child extends Parent{
public void testIt(){
System.out.println(x); // compiles?
}
}
Definition
yes because its in the same package
Term
Protected Keyword (used for inheritance) [Packaged kids]:
used to define a member, any subclass of the class declaring the member can access it regardless if they are different packages. The subclass can only see the protected member through inheritance
Definition
YES because of inheritance
Term
Protected Keyword (used for inheritance) [Packaged kids]:
used to define a member, any subclass of the class declaring the member can access it regardless if they are different packages. The subclass can only see the protected member through inheritance
Definition
Compiler error, trying to access it
through parent reference!
Term
Local variables only have___ access


public int x = 7
protected int x = 7
final int x = 7
static int x = 7
Definition
final int x = 7
Term
Abstract Methods: declared but not implemented, required to be ________

a. extended
b. declared
Definition
extended
Term
public abstract class A{
abstract void foo();
}

class B extends A{
void foo(int I){ //will this line compile?

}
}
Definition
won’t compile this is overloaded!
Term
abstract final Method? legal?
Definition
No, contradicts
Term
abstract synchronized, strictfp, native void thisMethod(); ?
lega?
Definition
NO explains implemenation detils...
Term
abstract static doStuff();
Is this legal?
Definition
NO
Term
Synchronized – only applies to ______, can be accessed only one thread at a time.


classses
methods
Definition
methods
Term
public/private/default/protected synchronized method ?
legal?
Definition
YES
Term
synchronized final method?
compiles
Definition
YES
Term
synchronized final class?
compiles?
Definition
NO because synchronized only applies to methods
Term
Native used only for ____implemented in a platform independent way,
never combined with abstract.

classes or methods?
Definition
methods
Term
Strictfp => used for both Classes and what kind of methods:
Definition
non-abstract methods
Term
For instance variables
what are the 3 legal types?
Definition
final, transietn, volatire
Term
For instance variables, what
is illegal? (4)
Definition
abstract, syyncrhoized, strictfp, natvie
Term
final instance variables need to be intilized by the time the _______ method appears.
Definition
constructor
Term
Do local variables get default values?
Definition
NO
Term
For objects, can be modified, but reference variable (bit pattern can’t be changed).
final Date d = new Date();
d.setYear(2001) //compiles
d = new Date (); //can’t compile why not?
Definition
because we’re changing the bit pattern. (ref variable)
Term
What does this print
i.e. class Frog{
static int frogCount = 0;
public Frog() {
frogCount +=1;
}
public static void main (String [] args){
new Frog();
new Frog();
System.out.println(frogCo
Definition
frogCount = 2!
Term
Source File Structure:

package com.geeks;
import java.util.* //wild card package import
import com.wicketlysmart.Foo; //Explicit class import.


WIll these compile
Definition
YES
Term
Why these not compile?

import java.util.ArrayList.*
import java.util;
Definition
import java.util.ArrayList.* => won’t compile because it is a class not package.
import java.util; //won’t compile since util is not a class.
Term
static void b(); //compile?
Definition
illegal, defines instance methods
Supporting users have an ad free experience!