“Introduction to Computer Science I” typically serves as a foundational course that introduces students to key concepts in computer science. Here’s an overview of what such a course might cover:
- Fundamental Concepts: This includes introducing students to the basic principles of computation, algorithms, and data structures. Topics might include variables, data types, operators, expressions, control structures (such as loops and conditionals), functions, and basic I/O.
- Programming Languages: An introduction to programming languages, focusing on one or more languages commonly used in introductory courses, such as Python, Java, or C++. This involves learning syntax, semantics, and basic programming constructs.
- Data Structures and Algorithms: This segment typically covers fundamental data structures like arrays, linked lists, stacks, queues, trees, and graphs. Students also learn about basic algorithms for searching, sorting, and traversing these data structures.
- Problem Solving and Algorithm Design: Emphasis is placed on problem-solving strategies and techniques, including algorithm analysis, recursion, and dynamic programming. Students learn how to approach problem-solving systematically and efficiently.
- Software Engineering Principles: Introducing students to software development methodologies, version control systems, debugging techniques, and best practices for writing maintainable and reusable code.
- Computer Architecture and Operating Systems: A high-level overview of computer architecture and how software interacts with hardware. Topics may include memory management, CPU scheduling, file systems, and processes.
- Introduction to Databases: An introduction to database concepts, including relational databases, SQL queries, database design principles, and basic database management systems.
- Ethical and Social Implications: Exploring ethical considerations related to computing, including privacy, security, intellectual property, and the impact of technology on society.
- Introduction to Web Development: Basic concepts of web development, including HTML, CSS, JavaScript, and introductory server-side programming.
- Additional Topics: Depending on the course and curriculum, additional topics such as networking, artificial intelligence, or computer graphics might be introduced at a basic level.
Overall, the goal of an Introduction to Computer Science I course is to provide students with a solid foundation in both theoretical and practical aspects of computer science, preparing them for more advanced coursework and careers in the field.
CS101: Introduction to Computer Science I Exam Quiz Answers
Question 1: 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?
- Radio
- Telegraph
- FORTRAN
- Vacuum tubes
Question 2: Which of the following subsets of English sentences is modeled by propositional logic?
- Declarative
- Exclamatory
- Imperative
- Question
Question 3: 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
Question 4: What is the technical term for the basic computer model?
- Architecture
- Flowchart
- Procedure
- Process
Question 5: What is a methodology?
- A 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
- A 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
Question 6: 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
Question 7: 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
Question 8: 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
Question 9: 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
Question 10: 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
Question 12: 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
Question 13: What data-types do the == and != relational operators apply to? Select one:
- Any Java object type
- Any primitive data type
- Only char and string types
- Only primitive numeric types
Question 14: 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
Question 15: 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
Question 16: Which of the following is a special data-type that is supported by Java?
- short
- int
- logic
- string
Question 17: 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
Question 18: What would the value of y be after this code is executed?
int y = 2;
y = y – –y;
- -2
- -1
- 0
- 1
Question 19: 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
Question 20: 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
Question 21: 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))
Question 22: 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&& B) || !B
- (A && B) || !A
- (A || B) && (!A || B)
- (B && !A) || (A && B)
Question 23: 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
Question 24: Which of the following statements acts as a control structure?
- int x;
- x = 1;
- if then
- puts
Question 25: 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
Question 26: 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
Question 27: 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
}
Question 28: In Java, how is information passed to an object?
- By class data
- By static data
- By local variables
- By method arguments
Question 29: 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
Question 30: 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
Question 31: 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 + ” “);
}
Question 32: Which of the following is Java standard I/O?
- System.read
- System.err
- System.util.scanner.nextInt()
- System.util.scanner.nextLine()
Question 33: 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
Question 34: 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
Question 35: 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
Question 36: 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
Question 37: 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
Question 38: Which of the following is a hardware component of the basic computer model?
- GUI
- RAM
- Cache
- Transistor
Question 39: 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
Question 40: 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
Question 41: 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
Question 42: Which of the following do procedural languages use to construct programs?
- Objects and messages
- Functions and operations
- Predicates and operations
- Data and control statements
Question 43: 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
Question 44: 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
Question 45: 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
Question 46: 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
Question 47: Which of the following is the most appropriate Java primitive type to use for scientific calculations involving very large numbers?
- binary
- decimal
- double
- long
Question 48: 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)
Question 49: 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);
};
Question 50: 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)
Question 51: 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))
Question 52: 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”);
}
Question 53: 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”);
}
- case 2
- label 3
- It would give a compile error
- It would give a runtime error
Question 54: 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
Question 55: 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)
}
Question 56: 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.
Question 57: 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);
Question 58: 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();
}
}
Question 59: When in the programming process does an exception occur?
- At run-time
- At design time
- At compile time
- During requirements specification
Question 60: 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
Question 61: 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
Question 62: 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
Question 63: Which of the following features of Java supports code reuse?
- Objects
- Network support
- Memory management
- Communication support
Question 64: 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
Question 65: 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
Question 66: 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
Question 67: 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
Question 68: What would the result of running int x += 1 be?
- 1
- x
- x+1
- An error
Question 69: 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
Question 70: 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
Question 71: 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];
Question 72: Which of the following Java code snippets would print this table?
A B C D
0 1
0 0 2
0 0 0 3
Answer: Option A
Question 73: 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?
- public static void main (String [] args) {
Car [] inventory = new Car [1000];
int [] i = {0, 1, 2, …, inventory. length – 1};
//assume the dots are the intervening digits
inventory [0] = new Car (“LCAR”, “Infinity”, 0, “in”);
inventory [1] = new Car (“FFAR”, “Expedition”, 0, “in”);
inventory [2] = new Car (“SFAR”, “Explorer”, 0, “in”);
…
//assume the dots represent the code to initialize the rest of inventory
for (int index: i) {
if (index >= 0 && index < inventory. length)
System.out.println(inventory[index]. idCar + ” “
+ inventory[index]. typeCar + ” ” + inventory[index]. modelCar
+ ” ” + inventory[index]. statusCar);
}
}
- public static void main (String [] args) {
Car [] inventory = new Car [];
int [] i = {0, 1, 2, …, inventory. length – 1};
//assume the dots are the intervening digits
inventory [0] = new Car (“LCAR”, “Infinity”, 0, “in”);
inventory [1] = new Car (“FFAR”, “Expedition”, 0, “in”);
inventory [2] = new Car (“SFAR”, “Explorer”, 0, “in”);
…
//assume the dots represent the code to initialize the rest of inventory
for (int index: i) {
if (index >= 0 && index < inventory. length)
System.out.println(inventory[index]. idCar + ” “
+ inventory[index]. typeCar + ” ” + inventory[index]. modelCar
+ ” ” + inventory[index]. statusCar);
}
}
- public static void main (String [] args) {
Car [] inventory = new Car [1000];
int [] i = {0, 1, 2, …, inventory. length – 1};
//assume the dots are the intervening digits
inventory [0] = new Car (“LCAR”, “Infinity”, 0, “in”);
inventory [1] = new Car (“FFAR”, “Expedition”, 0, “in”);
inventory [2] = new Car (“SFAR”, “Explorer”, 0, “in);
…
//assume the dots represent the code to initialize the rest of inventory
for (int index: i) {
if (i >= 0 && i < inventory. length)
System.out.println(inventory[i]. idCar + ” ” + inventory[i]. typeCar
+ ” ” + inventory[i]. modelCar + ” ” + inventory[i]. statusCar);
}
}
- public static void main (String [] args) {
Car [] inventory = new Car [1000];
int [] i = {0, 1, 2, …, inventory. length – 1};
//assume the dots are the intervening digits
inventory [0] = new Car (“LCAR”, “Infinity”, 0, “in”);
inventory [1] = new Car (“FFAR”, “Expedition”, 0, “in”);
inventory [2] = new Car (“SFAR”, “Explorer”, 0, “in”);
… //assume the dots represent the code to initialize the rest of inventory
for (int index: i) {
if (index >= 0 && index < inventory. length)
System.out.println(inventory[index]. idCar
+ ” ” + inventory[index]. typeCar + ” “
+ inventory[index]. modelCar + ” ” + inventory[index]. statusCar);
}
}
Question 74: 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
Question 75: Which of the following does Java use to implement encapsulation?
- Inheritance
- Constructors
- Polymorphism
- Declaration keywords
Question 76: 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
Question 78: 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
Question 79: 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);
Question 80: 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”);
}
Question 81: 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
Question 82: 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
Question 83: 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
Question 84: Today’s computers are considered to be part of which computer hardware generation, which enabled larger software abstractions? Select one:
- The second generation, which was characterized by transistors
- The 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
Question 85: Which of the following describes how computers can improve the processes involved in an information system? Select one:
- 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
Question 86: 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
Question 87: Which of the following statements is true? Select one:
- 123. is the same as “123F”
- A short type can represent 34567
- An int can represent about 8 digits of precision
- A float can represent about 7 digits of precision
Question 88: 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);
}
}
Select one:
- true
- false
- It would give a syntax error
- It would give a runtime error
Question 89: Which of the following Java logical expression is equivalent to NOT (A || B)? Select one:
- A & & B
- ! A & & B
- ! A & & B
- ! (A & & B)
Question 90: 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
Select one:
- A
- B
- (! A OR B) AND (A OR! B)
- (! A AND! B) OR (A AND! B)
Question 91: 5a.3. Which of the following is a control statement? Select one:
- import java.util.*
- long size = 1234567890;
- System.out.println(“Hello world!”);
- try {System.out.println(s. substring(start))};
Question 92: 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. Select one:
- import java.io. *;
import java. util. Scanner;
public class CarInventory {
public static void main (String [] args) {
Scanner fileScr;
Scanner keyBoardScr;
String fileName = “carInventory.txt”;
keyBoardScr = new Scanner (System.in);
try {
File carFile = new File(fileName);
fileScr = new Scanner(carFile);
} catch (IOException e) {
e. printStackTrace ();
}
String car = null;
if (fileScr.hasNext())
car = fileScr.next();
while (car! = null) {
System.out.println(car);
car = fileScr.next();
}
}
}
- import java.io. *;
import java. util. Scanner;
public class CarInventory {
public static void main (String [] args) {
Scanner fileScr;
String fileName = “carInventory.txt”;
keyBoardScr = new Scanner (System.in);
try {
File carFile = new File(fileName);
fileScr = new Scanner(carFile);
} catch (IOException e) {
e. printStackTrace ();
}
String car = null;
if (fileScr.hasNext())
car = fileScr.next();
while (car! = null) {
System.out.println(car);
car = fileScr.next();
}
}
}
- import java.io. *;
import java. util. Scanner;
public class CarInventory {
public static void main (String [] args) {
Scanner fileScr;
Scanner keyBoardScr;
keyBoardScr = new Scanner (System.in);
try {
File carFile = new File(fileName);
fileScr = new Scanner(carFile);
} catch (IOException e) {
e. printStackTrace ();
}
String car = null;
if (fileScr.hasNext())
car = fileScr.next();
while (car! = null) {
System.out.println(car);
car = fileScr.next();
}
}
}
- import java.io. *;
import java. util. Scanner;
public class CarInventory {
public static void main (String [] args) {
Scanner fileScr;
Scanner keyBoardScr;
String fileName = “carInventory.txt”;
Scanner keyBoardScr = new Scanner (System.in);
try {
File carFile = new File(fileName);
fileScr = new Scanner(carFile);
} catch (IOException e) {
e. printStackTrace ();
}
String car = null;
if (fileScr.hasNext())
car = fileScr.next();
while (car! = null) {
System.out.println(car);
car = fileScr.next();
}
}
}
Question 93: Which of the following is a key feature of Java? Select one:
- Machine independence
- Many low-level facilities
- An object model, like C++
- Manual memory management
Question 94: You are using the Java Development Kit to write a simple program. Which of the following file types will you save the program as? Select one:
- .html
- .java
- .jdoc
- .txt
Question 95: 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? Select one:
- C is an object
- CQ is an object
- CQ is a subclass of C
- CQ is a parent of class C
Question 96: 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? Select one:
- They have the same class data and behavior
- They have different class data and behavior
- They have the same class data and different behavior
- They have different class data and the same behavior
Question 97: Which of the following is the best definition of a primitive data-type? Select one:
- 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
Question 98: What would the value of x be after this code is executed?
float x = 1, y = 2;
x = x / 2;
Select one:
- 0
- 0.5
- 1/2
- 1
Question 99: 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? Select one:
- 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
Question 100: Which of the following is common to all Java loops? Select one:
- Conditions
- Counters
- Sentinels
- Updaters
Question 101: 8e.1. Suppose an integer value is input into a program where it is used as the divisor in a division computation. What is the best approach to address the risk of dividing by zero? Select one:
- Traditional error handling
- Default exception handling
- Java’s exception-handling model
- Terminate the program by calling System.exit
Question 102: 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);
}
}
Select one:
- 1.1
- integer 1.1
- It would give a run-time exception
- It would give a compile format error
Question 103: Which of the following technological developments made computers widely available to the public by lowering costs, increasing reliability, and improving usability? Select one:
- UNIX
- Transistors
- Vacuum tubes
- Integrated circuits
Question 104: Which of the following is a computer science principle that underlies the basic computer model? Select one:
- Composition of detailed components
- Decomposition into major components
- Hierarchy of components and subcomponents
- Language for forming instructions that control components
Question 105: Which of the following computer science principles underlies the programming life cycle? Select one:
- Abstraction
- Composition
- Decomposition
- Language