Home Certification CS101: Introduction to Computer Science 1 Exam Answers

CS101: Introduction to Computer Science 1 Exam Answers

0
CS101 Introduction to Computer Science 1 Exam Answers CS101 Introduction to Computer Science 1 Exam Answers CS101 Introduction to Computer Science 1 Exam Answers CS101 Introduction to Computer Science 1 Exam Answers CS101 Introduction to Computer Science 1 Exam Answers

CS101: Introduction to Computer Science 1 Exam Answers – Saylor Academy

  • Radio
  • Telegraph
  • FORTRAN
  • Vacuum tubes

Which of the following technological developments initiated the introduction of general purpose computers, used first for large-scale numerical calculations and later for commercial applications?

Which of the following subsets of English sentences is modeled by propositional logic?

  • Declarative
  • Exclamatory
  • Imperative
  • Question

Which of the following do logical statements, programs, and the execution of programs correspond to, respectively?

  • Design, implementation, and testing
  • Requirements, construction, and testing
  • Design, construction, and implementation
  • Requirements, design, and implementation

What is the technical term for the basic computer model?

  • Architecture
  • Flowchart
  • Procedure
  • Process

What is a methodology?

  • xA set of general steps needed to perform a stage of the system life cycle
  • A set of tools that supports the activities of each stage of the system life cycle
  • xA set of processes that guides the stages of the system development life cycle
  • A procedure that specifies detailed steps to perform a stage of the system life cycle

Programming languages are classified by the type of design model or paradigm that their constructs and features support. Which programming paradigm is Java based on?

  • Functional
  • Imperative
  • Logic
  • Object

How does one obtain a Java Development Kit (JDK), which is required for establishing a Java development environment?

  • Lease the JDK
  • Purchase a JDK license
  • Purchase a copy of JDK
  • Accept the JDK license terms

You are using the Java Development Kit to write a simple program, and are compiling a program called ‘sample’. Which of the following files would contain the Java bytecode?

  • sample.exe
  • sample.java
  • sample.class
  • sample.bytecode

Computers cannot replicate many human characteristics, but have aspects that make them good tools to assist people in performing tasks or solving problems. Which of the following is one of those aspects?

  • Creativity
  • Curiosity
  • Ethics
  • Logic

Compared to procedural programming, which of the following is true about object-oriented programming?

  • It Increases software development costs and improves software quality
  • It decreases software development costs and improves software quality
  • It increases software life-cycle costs and decreases coupling of implementation and use
  • It decreases software life-cycle costs and increases coupling of implementation and use

Which of the following best describes a class in object-oriented programming?

  • Data and related object-oriented functions
  • A data type for a set of object-oriented programs
  • An object-oriented construct for saving the state of an object
  • A program construct used to define objects that share common attributes

Given three Java classes A, B, and C that form this class hierarchy diagram:

A

|

B

|

C

Which of the following correctly represents the class hierarchy of the diagram?

  • B is a subclass of A, and C is a subclass of B
  • A is a subclass of B, and B is a subclass of C
  • A is a superclass of C, and B is a superclass of C
  • A is a superclass of B and C, and C is a subclass of A

In the first step of creating a Java application, a source program is written using an editor. Which of the following statements about this step is true?

  • Any text editor may be used to create a Java source program
  • Java source files can be of many file types, such as .txt, .java, .xml, or .docx
  • The file name of a Java source file must be the name of the only public class in the file
  • Word Processing software such as Microsoft Word is often used to create Java source programs

To use a single class in a Java package, which of the following must a program do?

  • Include the Java package in the Java command line
  • Include the Java package in the Javoc command line
  • Import the Java package anywhere in the java source file
  • Import the Java class at the beginning of the java source file

Which of the following is a special data-type that is supported by Java?

  • short
  • int
  • logic
  • string

Calculations involving values of a given data-type may give a result that is outside the finite range of the data-type. Which of the following Java calculations would give a result that is outside the range for its indicated data-type?

  • Using double, calculate 7 / 2
  • Using int, calculate 50000 + 5000000
  • Using short, calculate 1+2+3+……+200
  • Using unsigned int, calculate 100 – 5000

What would the value of y be after this code is executed?

int y = 2;

y = y – –y;

  • -2
  • -1
  • 0
  • 1

What would this code output after it is executed?

public class Practice {

    public static void main(String[] args) {

        String str = new String(“Technology through engineering to design.”);

        str = str.substring(11);

        System.out.println(str);

    }

}

  • Technology t
  • through engineering to design.
  • Technology through engineering to design.
  • A compile error, since a string is immutable

What data-types do the == and != relational operators apply to?

  • Any Java object type
  • Any primitive data type
  • Only char and string types
  • Only primitive numeric types

A student’s numeric grade is converted to a letter grade using a set of conditions. For example, if their grade is between 70 and 79, inclusive, then their grade is C. Which of the following code snippets accurately represents these conditions?

  • (grade >= 70) && (grade < 80)
  • (grade <= 80) && (grade > 70)
  • !(grade < 70) || !(grade >79)
  • !((grade < 70) || (grade >79))

A common logical operation is implication, which is often written as A → B. This is false when A is true and B is false, and true otherwise. Which of the following Java logical expressions is equivalent to A → B?

  • (A&amp;&amp; B) || !B
  • (A &amp;&amp; B) || !A
  • (A || B) &amp;&amp; (!A || B)
  • (B &amp;&amp; !A) || (A &amp;&amp; B)

Two logical expressions are equivalent if and only if which of the following are true?

  • They have the same form
  • They have the same values
  • They have the same truth tables
  • They have the same symbols in the same order

Which of the following statements acts as a control structure?

  • int x;
  • x = 1;
  • if then
  • puts

Engineers must measure visual acuity so that systems that involve visual output will be optimized for most of the population. Suppose you are projecting slides from a computer monitor, you want to choose a font size, which is dependent on the size of the classroom, that enables slides to be comfortably read by most students. Which of the following Java ‘if’ statements would output an optimal font size for a given room size and given visual acuity?

if (roomLength == 20) (if (visualAcuity – 1.0 < .001))

// checks for acuity equal to 1+-.001

    System.out.println(“Recommended font size for a room of length “

    + roomLength + ” is: ” +  DisplayPixel(roomLength, visualAcuity));

    //DisplayPixel is a function call that does the engineering calculation

if (roomLength == 20) if (visualAcuity – 1.0 < .001)

// checks for acuity equal to 1+-.001

    System.out.println(“Recommended font size for a room of length “

    + roomLength + ” is: ” +  DisplayPixel(roomLength, visualAcuity));

    //DisplayPixel is a function call that does the engineering calculation

if (roomLength == 20) {if (visualAcuity – 1.0 < .001)

// checks for acuity equal to 1+-.001

    System.out.println(“Recommended font size for a room of length ” +

    roomLength + ” is: ” +  DisplayPixel(roomLength, visualAcuity))};

    //DisplayPixel is a function call that does the engineering calculation

if (roomLength == 20) if (visualAcuity – 1.0 < .001)

// checks for acuity equal to 1+-.001

    {System.out.println(“Recommended font size for a room of length ” +

    roomLength + ” is: ” +  DisplayPixel(roomLength, visualAcuity))};

    //DisplayPixel is a function call that does the engineering calculation

The syntax of a switch statement includes sections like:

switch (expression)

{ case label1 :

Which of the following rules are correct for expression and label1?

  • Variables in expression and label1 can be global variables
  • label1 and the variables in expression must be of integer type
  • expression must evaluate to an integer and label1 must be an integer literal
  • If expression equals the variable label1, the statements of label1 are executed

What would be the best way to rewrite this code using a for loop?

int cost, ticket; //ticket is given a value of 1, 5, or 9 dollars

//for red, green, and blue tickets, respectively

switch (ticket) //ticket is drawn and is one of several winning tickets

{

    case 1:

        cost = ticket;

        System.out.println(“ticket cost $” + cost);

        System.out.println(“ticket wins $” + cost * 10);

        break;

    case 5:

        cost = ticket;

        System.out.println(“ticket cost $” + cost);

        System.out.println(“ticket wins $” + cost * 10);

        break;

    case 9:

        cost = ticket;

        System.out.println(“ticket cost $” + cost);

        System.out.println(“ticket wins $” + cost * 10);

}

int cost, ticket = 1; //value of ticket comes from System.in

for (; ticket != 0;) {

    cost = ticket;

    System.out.println(“ticket cost $” + cost);

    System.out.println(“ticket wins $” + cost * 10);

    ticket = value; // ticket given a value of 1, 5, 9 from input

    break;              

}

int cost, ticket; //value of ticket comes from System.in

for (; ticket != 0;) {

    cost = ticket;

    System.out.println(“ticket cost $” + cost);

    System.out.println(“ticket wins $” + cost * 10);

    ticket = 1; // ticket given a value of 1, 5, 9 from input

}

int cost, ticket = 1; //value of ticket comes from System.in

for (; ticket != 0;) {

    ticket = cost;

    System.out.println(“ticket cost $” + cost);

    System.out.println(“ticket wins $” + cost * 10);

    ticket = 1; // ticket given a value of 1, 5, 9 from input

    break;

}

int cost, ticket = 1; //value of ticket comes from System.in

for (;;;) {

    cost = ticket;

    System.out.println(“ticket cost $” + cost);

    System.out.println(“ticket wins $” + cost * 10);

    ticket = 1; // ticket given a value of 1, 5, 9 from input

}

In Java, how is information passed to an object?

  • By class data
  • By static data
  • By local variables
  • By method arguments

A method is called by writing the name of a method in a statement. What must this be preceded by?

  • The program name in which the method is defined and a dot
  • The name of the class in which the method is defined and a dot
  • The name of an instance of the class in which the method is defined and a dot
  • The keyword new followed by the name of a class constructor for the method

Which of the following statements about the use of arrays is correct?

  • An array is a java object
  • An array parameter is passed by reference
  • An array can be used to group different types
  • Components of an array are accessed by simple identifiers

Which of the following diagrams gives a correct logical representation of the memory allocation of a two-dimensional array? A box represents a location in the computer’s memory, a line entering a box indicates the value stored in the box, and a line whose end is near a boundary or on the boundary of a box indicates the address of the box.

Answer: Option D

Which of the following code snippets produces a different result than that of the others, given these definitions?

Car[] inventory = new Car[3];

inventory[0] = new Car(“LCAR”, “Infinity”, 5000, “in”);

inventory[1] = new Car(“FFAR”, “Expedition”, 23000, “in”);

inventory[2] = new Car(“SFAR”, “Explorer”, 32000, “in”);

for (Car findCar: inventory) {

    findCar.statusCar = “in”;

    System.out.print(findCar.typeCar + ” “);

    System.out.print(findCar.modelCar + ” “);

    System.out.println(findCar.statusCar + ” “);

}

int[] indexArray = { 0, 1, 2 };

for (int findCar: indexArray) {

    inventory[findCar].statusCar = “in”;

    System.out.print(inventory[findCar].typeCar + ” “);

    System.out.print(inventory[findCar].modelCar + ” “);

    System.out.println(inventory[findCar].statusCar + ” “);

}

Car[] indexArray = { inventory[0], inventory[1], inventory[2] };

for (int findCar: indexArray) {

    findCar.statusCar = “in”;

    System.out.print(findCar.typeCar + ” “);

    System.out.print(findCar.modelCar + ” “);

    System.out.println(findCar.statusCar + ” “);

}

Car[] indexArray = { inventory[0], inventory[1], inventory[2] };

for (Car findCar: inventory) {

    findCar.statusCar = “in”;

    System.out.print(findCar.typeCar + ” “);

    System.out.print(findCar.modelCar + ” “);

    System.out.println(findCar.statusCar + ” “);

}

Which of the following is Java standard I/O?

  • System.read
  • System.err
  • System.util.scanner.nextInt()
  • System.util.scanner.nextLine()

Data that is read or written by a Java program can be part of the program, or it can be stored on an external device such as a disk. When would you want to store data on an external disk?

  • When data used by a program is useful for other programs
  • When data used by a program is dependent on the program
  • When you want the program to write its data faster than to external file
  • When you want the program to read its data faster than from an external file

Which statement correctly describes how Java classes are applicable for retrieving external data?

  • The Scanner class can be used to read byte data
  • The Scanner class can be used to read input data
  • Data can only be read from a file using the FileReader class.
  • The Scanner class can only be used to read data from the keyboard

What would this code output after it is executed?

public class Main {

    public static void main(String[] args) {

        Object x[] = new Object[3];

        x[0] = new Integer(0);

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

    }

}

  • Hello World
  • It would give a null pointer exception
  • It would give an array store exception
  • It would give an out of bounds exception

Which lines of this code will be executed?

1  public class Main {

2    public static void main(String[] args) {

3      String s = “Tvhis is a string.”;

4      System.out.println(s);

5      int end = s.length();

6      int start = 18;

7      System.out.println(“length of string ” + end);

8      if (start > end)

9        throw new StringIndexOutOfBoundsException(“Exception: start

         of the substring is greater than the length of the substring “);

10

11     try {

12       System.out.println(s.substring(start));

13     } catch (StringIndexOutOfBoundsException e) {

14       System.out.println(e.getMessage());

15       System.exit(0);

16     }

17   }

18 }

  • 3, 7, 11
  • 3, 7, 9, 13
  • 3, 7, 11, 13
  • 3, 7, 9, 11, 13

How is semantics specified in propositional logic?

  • Each atomic statement is assigned the value True
  • Each atomic statement is assigned the value False
  • If an atomic statement is true, it is assigned the value True; otherwise it is False
  • A sequence of n atomic statements is assigned 2n permutations of True and False

Which of the following is a hardware component of the basic computer model?

  • GUI
  • RAM
  • Cache
  • Transistor

Which stages of the programming life cycle model are associated with synthesis activities, such as composing components to create the structure of a program?

  • Design and implementation
  • Implementation and Testing
  • Requirements and Design
  • Requirements and Testing

You are using the Java Development Kit to write a simple program. Which of the following commands would you use to run a Java program called sample?

  • run sample
  • java sample
  • run sample.class
  • java sample.class

Which of the following use cases would a computer be best for?

  • To make decisions
  • To discover new information
  • To execute sets of instructions
  • To recognize problems and create solutions

Which of the following do procedural languages use to construct programs?

  • Objects and messages
  • Functions and operations
  • Predicates and operations
  • Data and control statements

Which of the following best describes an object, in object-oriented programming?

  • A set of values for a data type
  • A data type and its associated programs
  • A construct that characterizes a member of a class
  • A set of constructs that communicate via messages

What command would you use to compile this Java source code from a file called Test.java?

class Hello {

    Public static void main(String[] args) {

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

    }

}

  • java Hello
  • javac Hello
  • java Test.java
  • javac Test.java

Which of the following best describes a Java package?

  • A collection of classes used by a program
  • A collection GUI tools used by a program
  • A collection of objects that make up a program
  • A collection of programs used by an application

Which of the following is the default size (that is, the maximum value) of the set of values having type int?

  • 231
  • 264
  • (232)-1
  • (264)-1

Which of the following is the most appropriate Java primitive type to use for scientific calculations involving very large numbers?

  • binary
  • decimal
  • double
  • long

Relationships of operands can usually be expressed in different ways. If x and y are of type int, then which of the following is equivalent to x <= y?

  • !(x > y)
  • -x <= -y
  • (x < y) && (x = y)
  • (x +1) >= (y + 1)

A university has a student database containing information on student enrollment. If you want a report of all enrolled students (whose status is e) who have at least 60 credits, which of the following relational expressions would you use?

if (status == ‘e’) {

    if (credits >= 60)

        System.out.println(“student:”, studentID);

};

if (status = ‘e’) {

    if (credits >= 60)

        System.out.println(“student:”, studentID);

};

(if status == ‘e’) {

    (if credits >= 60)

    System.out.println(“student:”, studentID);

};

if (status = ‘e’) {

    if (credits! < 60)

        System.out.println(“student:”, studentID);

};

A person can donate blood if their age is between 18 and 60 and if they have a minimum weight of 110 pounds. Which of the following Java expressions would most precisely select those who can be blood donors?

int age, weight;

(age >= 18 && age <= 60) && (weight >= 110))

long age, weight;

(age >= 18. && age <= 60.) && (weight >= 110.)

int ageInMonths ; long weight;

(ageInMonths >= 216 && ageInMonths <= 720) && (weight >= 110.)

int ageInMonths; long weight;

(ageInMonths >= 216 && ageInMonths <= 720) && (weight > 109.9999)

To give blood, someone must have a minimum weight of 110 pounds, be older than 17 years old, and have an iron level that is over 12. If the iron reading is 12, a second iron level test is conducted, and its result is used to determine eligibility for donation. Which of the following Java condition statements uses relational operations to express these eligibility conditions?

  • (weight >= 110) && (age >= 17) && (iron2 >12.0)
  • ((weight >= 110) && (age >= 17)) || ((iron1 >= 12.0) || (iron2 >= 12.0))
  • ((weight >= 110) && (age >= 17)) && ((iron1 >= 12.0) || (iron2 >= 12.0))
  • ((weight >= 110) && (age >= 17) && (iron1 >12.0)) || ((weight >= 110) && (age >= 17) && (iron2 >12.0))

In the United States, routes that run east-west are given even numbers, and those that run north-south are given odd numbers. Which of the following if statements would correctly test the direction of a route?

int routeNum = 95;

if (routeNum % 2 == 0)

    System.out.println(“Route ” + routeNum + ” runs east-west”)

else System.out.println(“Route ” + routeNum + ” runs north-south”);

int routeNum = 95;

if (routeNum % 2 == 0)

    System.out.println(“Route ” + routeNum + ” runs east-west”);

else System.out.println(“Route ” + routeNum + ” runs north-south”);

int routeNum = 95;

(if routeNum % 2 == 0)

    System.out.println(“Route ” + routeNum + ” runs east-west”);

else System.out.println(“Route ” + routeNum + ” runs north-south”);

int routeNum = 95;

if (routeNum % 2 == 0)

    System.out.println(“Route ” + routeNum + ” runs east-west”);

else {

    System.out.println(“Route ” + routeNum + ” runs north-south”);

}

What would this code output after it is executed?

int label = 2;

switch (label) {

    default: System.out.println(“default”);

    case 1:

        {

            System.out.println(“case 1”);

            switch (label) {

                case 1:

                    System.out.println(“case 11”);

                case 2:

                    System.out.println(“case 12”);

            }

        }

    case 2:

            System.out.println(“case 2”);

    case 3:

            System.out.println(“label 3”);

}

  • label 3
  • case 2
  • label 3
  • It would give a compile error
  • It would give a runtime error

How does Java object A send a message to Java object B?

  • A calls any method of B
  • A calls a public method of B
  • A calls one of its methods which interfaces with B
  • A calls a class script, which in turn shares data with B

This Java program uses generic words for identifiers, and can be used as a template for developing Java code. Which of the following would change the value of instanceData to 4?

public class ClassName {

    int instanceData = 7;

    public ClassName(int data) {

        instanceData = data;

    } // ClassName (data) – constructor

    public ClassName() {

        instanceData = 0;

    } // ClassName (data) – constructor

    public void ChangeInstanceData(int num) {

        instanceData = num;

    }

    // ChangeInstanceData (int num)

    public int getInstanceData() {

        return instanceData;

    } // getInstanceData ()

    public void reportData() {

        System.out.println(

            “value of instanceData: ” +

            getInstanceData());

    } // reportData ( )

} // ClassName

public class Main {

    public static void main(String[] args) {

        changeInstanceData(4);

    }

}

public static void main(String[] args) {

    instanceData = 4;

}

public static void main(String[] args) {

    ClassName obj = new ClassName();

    obj.instantData = 4;

}

public static void main(String[] args) {

    ClassName obj = new ClassName();

    obj.ChangeInstanceData(4)

}

Which of the following statements is correct about the syntax of an enhanced for-loop header iterating over the elements of an array?

  • An enhanced for-loop uses subscripts to incrementally access elements of an array.
  • An enhanced for-loop has a concise structure related syntax for iterating over an array.
  • An enhanced for-loop generates validity checks for indices used in the body of the loop.
  • An enhanced for-loop uses for-loop features together with structure related features to iterate over an array.

You want to display the heading “ID#  CUSTOMER  NAME” for a report. Which of the following Java code snippets would format the heading in this way?

String s1 = ” CUSTOMER”;

String s2 = ” NAME”;

String s3 = ” ID#”;

System.out.println(“%2$s %1$s”, “s1+s2”, “s3”);

String s1 = ” CUSTOMER”;

String s2 = ” NAME”;

String s3 = ” ID#”;

System.out.println( % 2 $s % 1 $s “, s1+s2, s3.;   

String s1 = ” CUSTOMER”;

String s2 = ” NAME”;

String s3 = ” ID#”;

String heading = String.format(“%2$s %1$s”, s1 + s2, s3.;

System.out.println(heading);

String s1 = ” CUSTOMER”;

String s2 = ” NAME”;

String s3 = ” ID#”;

String heading = String.format(“%2$s %1$s”, “s1+s2”, “s3”);

System.out.println(heading);

You work at a car rental company. CarInventory.txt is a character file that contains rental car data. Each line contains a carId, carType, carModel, carMileage, and carStatus. Which of the following code snippets would correctly append additional cars to the inventory?

import java.io.*;

class WriteCarFile {

    public static void main(String[] args) throws IOException {

        String carfileName = “CarInventory.txt”;

        writer.open();

        FileWriter writer = new FileWriter(carfileName, true);

        writer.write(“4 LCAR Audi 0 in\n”);

        writer.write(“5 FFAR Suburban 0 in\n”);

        writer.write(“6 PPAR Ford150 0 in\n”);

        writer.close();

    }

}

import java.io.*;

class WriteCarFile {

    public static void main(String[] args) {

        String carfileName = “CarInventory.txt”;

        FileWriter writer = new FileWriter(carfileName, false);

        writer.write(“4 LCAR Audi 0 in”);

        writer.write(“5 FFAR Suburban 0 in”);

        writer.write(“6 PPAR Ford150 0 in”);

        writer.close();

    }

}

import java.io.*;

class WriteCarFile {

    public static void main(String[] args) throws IOException {

        String carfileName = “CarInventory.txt”;

        FileWriter writer = new FileWriter(carfileName, true);

        writer.write(“4 LCAR Audi 0 in\n”);

        writer.write(“5 FFAR Suburban 0 in\n”);

        writer.write(“6 PPAR Ford150 0 in\n”);

        writer.close();

    }

}

import java.io.*;

class WriteCarFile {

    public static void main(String[] args)

    {

        String carfileName = “CarInventory.txt”;

        FileWriter writer = new FileWriter(carfileName, true);

        writer.write(“4 LCAR Audi 0 in\n”);

        writer.write(“5 FFAR Suburban 0 in\n”);

        writer.write(“6 PPAR Ford150 0 in\n”);

        writer.close();

    }

}

When in the programming process does an exception occur?

  • At run-time
  • At design time
  • At compile time
  • During requirements specification

8e.2. Which of the following statements correctly describes which methods must have a ‘throws’ or ‘throw’ clause?

  • Every method
  • A method that might cause an exception
  • Only the methods that have a ‘catch’ clause
  • A method that might cause an exception, and each method in its chain

The principle of bivalence says that the value assigned to a proposition that represents a simple declarative sentence. Which of the following can that sentence be?

  • ‘null’
  • Unspecified
  • False or True
  • False and True

Given propositions P and Q, when is R a valid deduction? P and Q ⇒ R

  • When it is not possible for P or Q to be true and R false
  • When it is not possible for P and Q to be true and R false
  • When it is not possible for P or Q to be false and R true
  • When it is not possible for P and Q to be false and R true

Which of the following features of Java supports code reuse?

  • Objects
  • Network support
  • Memory management
  • Communication support

Which of the following is a function of the JDK?

  • To test Java programs
  • To analyze Java programs
  • To compile Java programs
  • To document Java programs

Which of the following features make object-oriented programming popular?

  • It promotes platform independence
  • It promotes program interdependence
  • It allows for encapsulation of data and procedures
  • It has greater execution efficiency compared to older paradigms

Given the Java statement public static void main(String[] args), what are the reserved words in the statement?

  • public, static, String, void
  • args, public, static, String, void
  • main, public, static, String, void
  • args, main, public, static, String, void

Which of the following is true about the import statement?

  • It can be anywhere in the Java source file
  • It makes the names of external classes visible
  • It includes external classes in a java source file
  • Together with *, it makes a class and subclasses visible

What would the result of running int x += 1 be?

  • 1
  • x
  • x+1
  • An error

What would this code output after it is executed?

public class Practice {

    public static void main(String[] args) {

        String str = new String(“Technology to Engineering to Design”);

        System.out.println(str.charAt(10));

    }

}

  • y
  • null
  • blank
  • empty

What would this code output after it is executed?

public class SystemFeature {

    public static void main(String[] args) {

        int label = 1;

        switch (label) {

            case 1:

                System.out.println(“label 1”);

            case 2:

                System.out.println(“label 2”);

        }

    }

}

label 1

label 1

label 2

It would give a compile error

It would give a runtime error

Which of the following correctly declares a one-dimensional array array1 that has 4 components?

  • int array1[4];
  • int[4] array1;
  • int[] array1 = new int[4];
  • int array1[] = new Array[4];

Which of the following Java code snippets would print this table?

0                                             

0             1                             

0             0             2            

0             0             0             3

Answer: Option A

You work for a car rental company. Car is a class whose objects represent rental cars, each having the following attributes: idCar and mileageCar have int type, and typeCar, modelCar, and statusCar have String type. StatusCar is either “in” or “out”, indicating whether the car is available for rent or is not. Let Car(String x, String y, int z, String w) be a constructor for this class, where x corresponds to type, y corresponds to model, z corresponds to mileage, and w corresponds to status. Assume that count is a class variable that is incremented each time a new Car is constructed, and is used to assign values to idCar. Assume mileageCar is initialized to 0. Assume inventory is an array of car objects. Which of the following code snippets would print a list of cars in the inventory that satisfy a condition or set of conditions?

Answer: Option D

Which of the following tools would you use to input Java code into the JDK?

  • A text editor
  • The JRE interpreter
  • The JDK Java editor
  • Windows Visual Studio

Which of the following does Java use to implement encapsulation?

  • Inheritance
  • Constructors
  • Polymorphism
  • Declaration keywords

What would this code output after it is executed?

public class Try {

    public static void main(String[] args) {

        int x = 1, y = 2;

        double xx = 1.0, yy = 2.0;

        System.out.println(x > xx);

        System.out.println(y == yy);

    }

}

False

False

False

True

True

False

True

True

A program consists of a sequence of various types of statements that process input data to produce output data. Each type of statement is used as part of a process, an input, or an output. How do control statements relate to each of these aspects?

  • They define the data that is stored
  • They change the data that is stored
  • They determine the order in which code is executed
  • They declare the name and type of data that is input

Looping structures are included in most, if not all, programming languages. They are one of the earliest techniques for reusing code. Which of the following programming activities is likely to use loops?

  • Transacting an online bank deposit
  • Performing a computation specified by an equation
  • Selecting the next statement to execute based on a condition
  • Executing a block of code repetitively using different data values

Which of the following is the correct call to the method that gets the length of the string “John”?

String str = “John”;

str.length();

String str = “John”;

String.length(str);

String str = new String(“John”);

length(str);

String str = new String(“John”);

String.length(str);

You work for a car rental company. Suppose that inventory is an array that represents a set of cars available for rent, where each car has 3 components of information: type, model, and status. An example representation for a car is: “mid”, “Chv”, “in”, which represents a midsize Chevrolet and is in and available for rent. Which of the following code snippets would search the inventory for this car?

for (int row, col = 0; row < 100 and col < 3; row++) {

    if (inventory[row][1] == “mid” &&

        inventory[row][2] == “chv”);

}

for (int row, col = 0; row < 100 and col < 3; row++) {

    if (inventory[row][col] == “mid” &&

        inventory[row][col + 1] == “chv”);

}

for (int row, col = 0; col < 3 && row < 100; col++) {

    if (inventory[col][row] == “mid” &&

        inventory[col][2] == “chv”);

}

for (int row, col = 0; col < 3 && row < 100; col++) {

    if (inventory[col][row] == “mid” &&

        Inventory[col][row + 1] == “chv”);

}

What would the result be when this code is executed and a user types mystring 123?

import java.util.Scanner;

import java.io.PrintWriter;

public class PrintSample {

    public static void main(String[] args) {

        Scanner sampleIn = new Scanner(System.in);

        PrintWriter sampleOut = new PrintWriter(System.out, false);

        String sIn = sampleIn.next();

        System.out.println(sIn);

        sampleOut.println(sIn);

        System.out.println(“ok”);

        int iIn = sampleIn.nextInt();

        System.out.println(iIn);

        sampleOut.close();

    }

}

mystring

ok

123

mystring

mystring

mystring

ok

123

123

mystring

123

ok

123

It would give an execution error

What is the process in Java for creating and writing a text file?

define and allocate a file object

use the object’s write method to write string fields

use the object’s close method to close the file

construct a FileWriter object with a file name

use the objects write method to write character lines to the file

use the objects close method to close the file

declare a file name

construct a FileWriter object with that defined file name

use a FileWriter method to write string lines to the file

use a FileWriter method to close the file

define a file name

use the FileWriter open method to allocate a file with that name

use the FileWriter write method to write byte to that file

use the FileWriter close method to close the file

In using the Scanner class for reading a text file, which of the following statements correctly describes the role of each of these classes?

  • Scanner methods can be used to read a text file
  • A File class object is attached to a Scanner object
  • A File class method can be used to check for end of file
  • Scanner.class methods do not throw file related checked exceptions

Which of the following is a computer science principle that underlies the basic computer model?

  • Composition of detailed components
  • Decomposition into major components
  • Hierarchy of components and subcomponents
  • Language for forming instructions that control components

Which of the following computer science principles underlies the programming life cycle?

  • Abstraction
  • Composition
  • Decomposition
  • Language

You are using the Java Development Kit to write a simple program. Which of the following file types will you save the program as?

  • .html
  • .java
  • .jdoc
  • .txt

In Java, a string is which of the following?

  • A class
  • An array
  • An object
  • A primitive data type

What would this code output after it is executed?

public class Try {

    public static void main(String[] args) {

        boolean x = false, y = false;

        if (x == y)

            System.out.println(true);

        else System.out.println(false);

    }

}

  • true
  • false
  • It would give a syntax error
  • It would give a runtime error

Which of the following Java logical expression is equivalent to NOT (A || B)?

  • A &amp;&amp; !B
  • !A &amp;&amp; B
  • !A &amp;&amp; !B
  • !(A &amp;&amp; B)

What types of ‘if’ statements are there in Java?

  • ‘do’ statements
  • ‘do’ and ‘do – until’ statements
  • ‘while’, ‘do – until’, and ‘for’ statements
  • ‘while’, ‘do – while’, and ‘for’ statements

Which of the following is common to all Java loops?

  • Conditions
  • Counters
  • Sentinels
  • Updaters

You work at a car rental company. CarInventory.txt is a character file that contains rental car data. Each line contains a carId, carType, carModel, carMileage, and carStatus. Which of the following code snippets correctly reads this text file using the Scanner class? Assume the input text file exists.

Answers: Option A or B

Which of the following is a key feature of Java?

  • Machine independence
  • Many low level facilities
  • An object model, like C++
  • Manual memory management

Which of the following describes how computers can improve the processes involved in an information system?

  • They improve the performance of the process
  • They remove the people involved in the process
  • They generate ideas for how to streamline the process
  • They decrease the amount of data involved in the process

Which of the following is the best definition of a primitive data-type?

  • A category of data that is part of the Java language
  • A user-defined class that declares a set of variables
  • A user-defined object that declares a set of identifiers
  • A piece of information needed by the Java Virtual Machine

What would the value of x be after this code is executed?

float x = 1, y = 2;

x = x / 2;

  • 0
  • 0.5
  • 1/2
  • 1

Modern programming languages contain Boolean types and Boolean operators (AND, OR, NOT) for constructing complex Boolean expressions that comprise a logical model. What do we call this model?

  • Propositional logic
  • First-order predicate logic
  • Second-order predicate logic
  • Higher-order predicate logic

What would this code output after it is executed?

public class Main {

    public static void main(String[] args) {

        char[] array1 = {

            ‘a’,

            ‘b’,

            ‘c’

        };

        System.out.println(array1[0]);

    }

}

  • a
  • ‘a’
  • It would give a compile error
  • It would give a runtime error

Which of the following minimal Java logical expressions has a meaning that is given by column C in this truth table?

A             B             C

0             0             0

0             1             1

1             0             0

1             1             1

  • A
  • B
  • (!A OR B ) AND (A OR !B)
  • (!A AND !B) OR (A AND !B)

5a.3. Which of the following is a control statement?

  • import java.util.*
  • long size = 1234567890;
  • System.out.println(“Hello world!”);
  • try {System.out.println(s.substring(start))};

If C is the set of all coins, CQ is the set of all quarters, and Q is a 1904 quarter, which of the following is a correct statement?

  • C is an object
  • CQ is an object
  • CQ is a subclass of C
  • CQ is a parent of class C

What would this code output after it is executed?

public class Main {

    public static void main(String[] args) {

        double decimalNumb = 1.1;

        System.out.printf(“integer %d”, decimalNumb);

    }

}

  • 1.1
  • integer 1.1
  • It would give a run-time exception
  • It would give a compile format error

Today’s computers are considered to be part of which computer hardware generation, which enabled larger software abstractions?

  • The second generation, which was characterized by transistors
  • xThe third generation, which was characterized by integrated circuits
  • The fourth generation, which was characterized by very-large-scale integration
  • The fifth generation, which was characterized by massive parallel hardware

Which of the following methods would calculate the circumference of a circle (which is 2 times pi times the radius of the circle), starting with this given code?

public class Circle {

    public double pi = 3.141592;

    public double radius;

    public Circle(double r) {

        radius = r;

    }

}

public double circum(r) {

    double c;

    return c = 2 * pi * radius;

};

public double circum(radius) {

    double c = 2 * pi * radius;

};

public double circum() {

    double c;

    return c = 2 * pi * radius;

};

public void circum() {

    double circum = 2 * pi * radius;

    System.out.println(“arrived ” + circum);

};

Given the Java statement class E extends class F, assuming E and F are different classes, what could most likely be said about the objects of E and F?

  • They have the same class data and behavior
  • xThey have different class data and behavior
  • They have the same class data and different behavior
  • They have different class data and the same behavior

LEAVE A REPLY

Please enter your comment!
Please enter your name here