CSE205

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/61

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

62 Terms

1
New cards
  1. Evaluate the following code to determine the output.

int age = 3;
String fname = "bob";
String lname = "smith";
int inches = 67;
System.out.printf("Mr. %s (first name %s) is %d inches tall and %d years old", lname, fname, inches, age);

Mr. smith (first name bob) is 67 inches tall and 3 years old

2
New cards

In Java, the mechanism by which a subclass provides a specific implementation of a method that is already defined in its superclass is known as:

method overriding

3
New cards

The process of rearranging the elements of an array in a specific order (e.g., ascending or descending) is known as a(n) _____.

sorting

4
New cards

Which of the following would correctly declare and instantiate an array of 3 String elements?

  1. String[] names = new String[2];

    • String names[] = new String[3];

    • String[] names = new String[4];

    • String names = new String[3];

    • String[3] names = new String[];

    • String[] names = new String[];

    • String names[] = new String[];

    • String[3] names = new String[3];

    • String[] names = new String[3];

    • String[3] names = new String[3];

String names[] = new String[3];

String[] names = new String[3];

5
New cards

If a primitive type variable is not explicitly initialized, it will store _____.

a default value based on its type

6
New cards
  1. What will this small program output?

class Main {
    public static void foo() {
        x = 4;
    }
    public static int x = 12;

    public static void main(String[] args) {
        int x = 24;
        foo();
        System.out.println(x);
    }
}

24

7
New cards

What will be the output of the following program?

import java.util.ArrayList; 
public class OutputQuestion2 { 
    public static void main(String[] args) { 
        ArrayList<String> colors = new ArrayList<>(); 
        colors.add("Red"); 
        colors.add("Green"); 
        colors.add("Blue"); 
        colors.remove("Green"); 
        System.out.println(colors); 
    } 
}

[Red, Blue]

8
New cards

What is the output of this Java program?

class Driver {
 public static void main(String[] args) {
   foo(4);
   bar(4);
 }
 static void foo(int a) {
   System.out.print(a);
 }
 static void bar(int a) {
   foo(a + 1);
   System.out.print(a);
 }
}

 

454

9
New cards

What is the output of this Java program?

class Driver {
 public static void main(String[] args) {
   int a = foo(6);
   int b = bar(a);
 }
 static int foo(int a) {
   a = bar(a - 3);
   System.out.print(a);
   return a;
 }
 static int bar(int a) {
   a = a - 1;
   System.out.print(a);
   return a + 2;
 }
}


243

10
New cards

What is the output of this Java program?

class Driver {
 public static void main(String[] args) {
   int a = 9;
   int b = 4;
   int c = 5;
   int x = 1;
   if (a > 5) {
     x = x + 900;
   }
   if (b > 9) {
       x = x + 40;
   }
   if (c < 9) {
       x = x + 5;
   }
   System.out.print(x);
 }
}

906

11
New cards

What will be the output of this code?

class Driver {

 public static void main(String[] args) {
   int a = 8;
   int b = 1;
   int c = 5;
   int x = 16;
   if (a + b < c) {
       x = 8;
   } 
   else {
       x = 15;
   }
   System.out.print(x);
 }
}

15

12
New cards

What will this small program output?

class Driver {
 public static void main(String[] args) {
   int a = 7;
   int b = 18;
   int c = 8;
   int x = 4;
   if (a <= b) {
       if (b > c) {
           x = 2;
       } else {
           x = 2;
       }
   } else {
       if (b <= c) {
           x = 17;
       } else {
           x = 19;
       }
   }
   System.out.print(x);
 }
}

2

13
New cards

What is the output of this Java program?

class Driver {
 public static void main(String[] args) {
   int a = 99;
   for (int i = 1; i < 4; i++) {
     a = a - i;
   }
   System.out.print(a);
 }
}

93

14
New cards
  1. When there are no remaining references to an object in your Java program, the object will be destroyed.

    1. True

    2. False

 

True

15
New cards

True or False: In Java, an int type variable can hold decimal values.

  1. True

  2. False

False

16
New cards

In Java, arrays are reference types.

True

17
New cards
  1. Which of the following would correctly declare and instantiate an array of 4 ints?

    1. int[] values = new int[6];

    2. int[] values = int[6];

    3. int[] values = int[5];

    4. int[6] values = new int[6];

    5. int[5] values = new int[];

    6. int[5] values = new int[5];

    7. int[] values = new int[5];

    8. int[] values = int[4];

    9. int[] values = new int[4];

    10. int[6] values = new int[];

    11. int[4] values = new int[4];

    12. int[4] values = new int[];

    13. None of the answers

int[] values = new int[4];

18
New cards

Given the following declaration:

int[] values = {16,71,75,67,55,75,45,16,51,19};

Evaluate the following expression:

Values[8]

51

19
New cards

What is the output of the following code?

int[] values = {82,11,15,25,17,69,61,88,80,36};
int i = 2;
System.out.println(values[i + 2] + 5);

22

20
New cards
  1. Which of the following loops would correctly add 1 to all but the first element in values? Choose all that apply

    1. for (int j = values.length - 1; j >= 0; j--){values[j]++;}

    2. for (int j = values.length; j > 0; j--){values[j]++;}

    3. for (int j = 1; j < values.length - 1; j++){values[j]++;}

    4. for (int j = 0; j < values.length - 1; j++){values[j]++;}

    5. for (int j = 0; j < values.length; j++){values[j]++;}

    6. for (int j = values.length; j >= 0; j--){values[j]++;}

    7. for (int j = 2; j < values.length; j++){values[j]++;}

    8. for (int j = values.length + 1; j > 0; j--){values[j]++;}

    9. None of the answer

None of the answers

21
New cards

Which keyword is used in Java to create an object?

  1. new

  2. create

  3. make

  4. build

  5. None of the answers

new

22
New cards

A name defined in an outer scope is also available in all blocks nested inside that scope.

  1. True

  2. False

true

23
New cards
  1. Given the following class definition, which statements will instantiate an object of this class? Choose all that apply.

class Restaurant {
 private String name;
 private int rating;
 public Restaurant() {
   this.name = "no name";
   this.rating = 0;
 }
 public Restaurant(String initName, int initRating) {
   this.name = initName;
   this.rating = initRating;
 }
}
  1. new Restaurant rr = Restaurant();

    • new rest = Restaurant("Taco Bell", 3);

    • new Restaurant myRest = Restaurant();

    • Restaurant diner = new Restaurant();

    • Restaurant r = new Restaurant("Chili's", 5);

    • Restaurant chilis = new Restaurant("Chili's");

    • Restaurant fastFood = new Restaurant("Taco Bell", 3);

    • Restaurant diner = new Restaurant(5, "Chili's");

    • String restaurant = new Restaurant("Taco Bell", 3);

    • None of the answers

Restaurant diner = new Restaurant(); Restaurant r = new Restaurant("Chili's", 5); Restaurant fastFood = new Restaurant("Taco Bell", 3)

24
New cards
  1. In Java what keyword does an object use to refer to itself?

    1. this

    2. self

    3. me

    4. None of the answers

    5. my


this

25
New cards

Evaluate the following code to determine the output.

class Foo {
   public int i = 73;
   public Foo(int i) {
       this.i = i;
   }
   public static void main(String[] args){
        Foo x = new Foo(23), y = new Foo(59);
        x.i = y.i;
        y.i = 67;
        System.out.println(x.i);
   }
}

59

26
New cards
  1. The object oriented element that allows us to build more complex objects out of simpler objects is.

    1. Inheritance

    2. Message Passing

    3. Composition

    4. Polymorphism

    5. Data Hiding

    6. Encapsulation

composition

27
New cards
  1. Which object oriented element is used to define "has a" relationships?

    1. Data Hiding

    2. Inheritance

    3. Composition

    4. Encapsulation

    5. Message Passing

    6. None of the answers

    7. Polymorphism

composition

28
New cards

Members that are public in the derived class are visible in the super class.

  1. True

  2. False

False

29
New cards

Constructor methods are not inherited.

  1. True

  2. False

True

30
New cards
  1. How many methods (including constructors) take no arguments?

Refer to the following UML Class Diagram when answering this question.

Pet

- name : int

- age : String

- hasFleas : Boolean

+ licenseNumber : String

+ weight : float

+ Pet()

+ Pet(name : String, age : int)

+ getName() : String

- setName(name : String) : void

+ getAge() : int

+ setAge(age : int) : void

+ speak() : void

- scratch(itches : int) : void

+ toString() : String

5

31
New cards
  1. How many public members does this class have  (not including constructors)?

Refer to the following UML Class Diagram when answering this question.

Pet

- name : int

- age : String

- hasFleas : Boolean

+ licenseNumber : String

+ weight : float

+ Pet()

+ Pet(name : String, age : int)

+ getName() : String

- setName(name : String) : void

+ getAge() : int

+ setAge(age : int) : void

+ speak() : void

- scratch(itches : int) : void

+ toString() : String

7

32
New cards

A java application always contains a single method. What is this method called?  Give the signature of this method.

public static void main(String[] args)

33
New cards

How is data given to this parameter?

public static void main(String[] args)

Through a command line interface. For example: java someCompiledClass data

34
New cards

Identifiers can only begin with?

Letters, underscores, dollar sign

35
New cards

Is Java a Functional Language, Procedural Language, Object-Oriented Language, or Logic Language?

Object-Oriented

36
New cards

What does API stand for?

Application programming interface

37
New cards

What needs to be done to use the scanner class?

import java.util.Scanner;

38
New cards

How would you print out “Hello World”?

System.out.println(“Hello World”);

39
New cards

What’s the difference between println, and print.

println ends with a line break

40
New cards

What is Encapsulation?

The hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed. The variables contained in an object should be modified only within the object

41
New cards

What are the three accessvisibility modifiers, and what is their scope?

public: can be accessed from anywhere

private: can only be accessed from inside the class

protected: the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

42
New cards

What is the purpose of toString()?

The purpose is to give a human readable representation of the object.

43
New cards

What is an accessor method?

An accessor method can be defined to access private variable in the class since private data cannot be accessed from outside of the class.

44
New cards

What is a mutator method?

A mutator method can be defined to modify private variable in the class.

45
New cards

What is a constructor?

A constructor is a special method that is used to create a new object.

46
New cards

Define a constructor with two parameters int x, y for the class Point.

public Point(int x, int y){ this.x = x; this.y = y;}

47
New cards
  1. Which method signatures would give an error if all methods were defined in a single class?

    1. public int calc(int num1, int num2){ … }

    2. public double calc(int num1, int num2){ …}

    3. public int calc(double num1, int num2){ … }

    4. public int calc(int num3, int num4){ … }

    5. public int calc(int num1, int num2, int num3){ … }

public double calc(int num1, int num2)
public int calc(int num3, int num4)

48
New cards

What is the difference between an instance variable, and a local variable.

Instance variables are global to all instance methods of the class, local variables are only visible to the method scope in which it is defined

49
New cards

The this reference refers to the ______ of the object.

instance

50
New cards

What is the keyword that associates with a variable or method that belongs to the instead of an object?

static: This keyword is used to associate a variable or method with the class itself, rather than with instances (objects) of that class. This means that the variable or method can be accessed without creating an object of the class.

51
New cards

Do all classes need a main method?

No, only one class should have a main method which is the entry point to your program.

52
New cards

What are four distinct characteristics of an object-oriented language?

Polymorphism – Deals with one entity being able to represent multiple types of entities method overloading and inheritance can fall under this

Inheritance – Borrowing traits from other classes

Encapsulation – An object is self contained and sufficient

Abstraction – Black Box all you need to know is what to put in and what your going to get out.

53
New cards

What is a wrapper class?

A wrapper class represents a particular primitive data type. (primitive data types – int, double, char, boolean, and so on)

54
New cards

Define a variable fmt that formats a decimal so that 3.1 shows 3.10, 3.11 shows 3.11, 3.111 shows 3.111, and 3.1114 shows 3.111.

. DecimalFormat fmt = new DecimalFormat("0.00#");

55
New cards

Why use inheritance? 

To extend the functionality of a currently implemented class. Inheritance allows modeling of a system in a hierarchical way, and giving a programmatic way of representing a relationship with inheritable properties.

56
New cards

What is the super reference? What does the constructor for the Dictionary class look like?

The super reference is a reference to the parent class. The Dictionary class would need a constructor that includes a super reference to the parent Book class constructor, such as: public Dictionary( ) { super( ); language = "?"; }

57
New cards

What is an abstract class and abstract method? Give an example of an abstract method definition. 

 

An abstract class cannot be instantiated and contains abstract methods (as well as possibly, non-abstract methods).

An abstract method is a method header without a method body. This is used for inheritence purposes. A child class can then fill in the method body of the parent's abstract method.

Example: public abstract void exampleMethod( ); //note the semi-colon

58
New cards
  1. Write the output generated by the following program. Round up the outputs to the 2nd place after decimal.

 public class Point{
         private double x;
         private double y;
         public Point(double x, double y){
                     this.x = x;
                     this.y = y;
         }
         public double getX(){
                     return x;
         }
         public double getY(){
                     return y;
         }
         public Point movePoint(Point pt){
         Point temp = new Point(getX()+pt.getY(), getY() - pt.getX());
            return temp;
         }
         public static void main(String[] args){
                     Point a = new Point(3.8,1.7);
                     Point b = a.movePoint(a);
                     Point c = b.movePoint(b);
                     System.out.println("1. " + a.getX() );
                     System.out.println("2. " + a.getY() );
                     System.out.println("3. " + b.getX() );
                     System.out.println("4. " + b.getY() );
                     System.out.println("5. " + c.getX() );
                     System.out.println("6. " + c.getY() );
         }
}

Output:

a. 3.8

b. 1.7

c. 5.5

d. -2.1

e. 3.4

f. -7.6

59
New cards

What does the following program output?

public class Example {
               public static int functionOne(int x, int y) {
                        x = x * 2;
                        y = y + x;

                        return y;
                }

                public static int functionTwo(int x, int y) {
                        return y - x;
                }

                public static String makeWords( ) {
                        return "Final output is: ";
                }

                public static void main(String[] args) {

                        String words = "?";

                        int a = 0;
                        int b = 4;
                        int c = 2;

                        a = functionOne(b, b);
                        System.out.println(a);

                        a = functionTwo(a, c);
                        System.out.println(a);

                        b = functionOne(b, a);
                        words = makeWords( );
                        words.concat(String.valueOf(b));

                        System.out.println(words + b);
                }
        }

Output:

12

-10

Final output is: -2

60
New cards

How do you define an array of integers?

int[] numbers = {0, 1, 2, 3};

61
New cards

Define an ArrayList object named "numbers" that contains integers?

ArrayList numbers = new ArrayList( );

62
New cards

Add the numbers 4 and 3 to the ArrayList. Then retrieve the item at index 0

numbers.add(4);

numbers.add(3);

System.out.println(numbers.get(0));