CSE 310 ARIZONA STATE UNIVERSITY -
FOR DATA STRUCTURE AND ALGORITHM -
ASU PRACTICE EXAMINATION 2026
QUESTIONS WITH ANSWERS GRADED A+
◍ Computers are machines that.
Answer: execute programs
◍ price - status0 - ""22 - "inexpensive" - "reasonable".
Answer: 68) Assuming that a user enters 22 as the price of an object, which
of the following hand-trace tables is valid for the given code snippet?int
price = 0;String status = "";Scanner in = new
Scanner(System.in);System.out.print("Please enter object's price: ");price =
in.nextInt();if (price >= 50){ status = "reasonable"; if (price >= 75) { status
= "costly"; }}else{ status = "inexpensive"; if (price <= 25) { status =
"reasonable"; }}
◍ How do you extract the first 5 characters from the string str?a) substring(str,
5)b) substring.str(0, 5) c) str.substring(5) d) str.substring(0, 5).
Answer: d) str.substring(0, 5)
◍ How do programmers find exceptions and run-time errors?.
Answer: Testing by running the program with a variety of input values
◍ what is the output of the following java program?Class Driver { public static
void main(String[] args) { int a = 3; int b = 5; if (a>b || a * 2 > b)
System.out.print(a); else System.out.print(b); }}a. 3b. 5c. 2d. 8e. none of
these.
Answer: a
◍ What is wrong with the following code snippet?int size = 42;cost =
, 9.99;System.out.println("size = " + size);System.out.println(" cost = " +
cost);a) The code snippet uses a variable that has not yet been initialized.b)
The code snippet uses a variable that has not been declared.c) The code
snippet attempts to assign a decimal value to an integer variable.d) The code
snippet attempts to assign an integer value to a decimal variable..
Answer: b) The code snippet uses a variable that has not been declared.
◍ 90) Which statement is true about the code snippet below?public static void
main(String[] args) { Scanner in = new Scanner(System.in);
ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while
(in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; }}a) The
code adds all input numbers to the array listb) The code has compile-time
errorc) The array list is full after 100 numbers are enteredd) The code has a
run-time error.
Answer: d) The code has a run-time error
◍ In order to translate a Java program to a class file, the computer needs to
have software called a(n).
Answer: compiler
◍ What kind of error is it when your program has a syntax error?.
Answer: Compile-time error
◍ 40) Which statement about instance variables is correct?a) Instance
variables must be declared within a method. b) Instance variables must be
declared inside the class, but outside any method. c) Instance variables may
be declared inside the class, or inside a method.d) Instance variables must be
declared outside the class.
Answer: b) Instance variables must be declared inside the class, but outside
any method
◍ which of the following loops would correctly add 1 to all but the first
element in values?a. for (int j = 1; j < values.length; j++) values[j]++;b. for
(int j = 0; j < values.length; j++) values[j]++;c. for (int j = 1; j <
values.length - 1; j++) values[j]++;d. for (int j = 0; j < values.length - 1; j++)
values[j]++;e. for (int j = values.length - 1; j >0; j--) values[j]++;f. for (int j
, = values.length; j >0; j--) values[j]++;g. for (int j = values.length; j >=0; j--)
values[j]++;h. for (int j = values.length - 1; j >=0; j--) values[j]++;i. none of
these.
Answer: a, e
◍ 34) What is the output of the code snippet given below?String s =
"abcde";int i = 1;do{ if (i > 1) { System.out.print(s.substring(i, i + 1));
}}while (i < 5);a) No outputb) No output (infinite loop)c) abcded) bcde.
Answer: Answer: b) No output (infinite loop)
◍ Which one of the following is a correct representation of the given
mathematical expression in Java?a + b----- 2a) a + b % 2b) a + b / 2 c) a + (b
/ 2)d) (a + b) / 2.
Answer: d) (a+b) / 2
◍ Which object oriented element is best defined as "an object should have
complete authority over its responsibilities"?.
Answer: Answered: Inheritance, Encapsulation
◍ What is the value inside the value variable at the end of the given code
snippet?public static void main(String[] args){ int value = 3; value = value -
2 * value; value++;}a) -2b) 0c) 2d) 4.
Answer: a) -2
◍ 83) Insert the missing code in the following code fragment. This fragment is
intended to implement a method to set the state of the object.public class
Motor{ public static final STOPPED = 0; public static final PAUSED = 1;
public static final RUNNING = 2; private int motorState; . . . public void
stopMotor() { ________ }}a) motorState++;b) motorState = STOPPED;c)
motorState = PAUSED;d) motorState = RUNNING;.
Answer: b) motorState = STOPPED;
◍ In order for Java to achieve protability.
Answer: compiled Java programs contain instructions for a virtual machine
◍ 31) Which of the following is the correct code snippet for throwing a pair of
dice to get a sum of the numbers on two dice between 2 and 12 with
, different probabilities?a) (int) (Math.random() * 6 + 1)b) (int)
(Math.random() * 12 + 1)c) (int) (Math.random() 6 + 1) + (int)
(Math.random() 6 + 1)d) (int) (Math.random() * 12 - 2).
Answer: Answer: c) (int) (Math.random() 6 + 1) + (int) (Math.random() 6 +
1)
◍ What is the difference between an editor and a compiler?.
Answer: An editor allows program files to be written and stored; a compiler
converts program files into an executable program
◍ 74) Which of the loop(s) test the condition after the loop body executes?
I. for loopI
I. while loopII
I. do loopa) I onlyb) II onlyc) III onlyd) II and III.
Answer: Answer: c) III only
◍ Can you have cycles in prim's algorithm?.
Answer: No!
◍ which of the following would instantiate an array of 5 integers in Java?
Choose all that apply..
Answer: new int[5]
◍ What is the output of this code snippet?int sum = 22;sum = sum +
2;System.out.print(sum); // sum = sum + 4;System.out.print(sum);a) 2424b)
2425c) 2428d) 2528.
Answer: a) 2424
◍ 34) Which type of method modifies the object on which it is invoked?a)
Constructor method. b) Static method. c) Accessor method. d) Mutator
method..
Answer: d) Mutator method.
◍ 87) What changes do you need to make in the following code snippet to
display "Let us learn Java" exactly 10 times?int i = 0;while (i <= 10){
System.out.println("Let us learn Java"); i++;}a) while (i < 9)b) while (i <
11)c) while (i < 12)d) int i = 1;.
FOR DATA STRUCTURE AND ALGORITHM -
ASU PRACTICE EXAMINATION 2026
QUESTIONS WITH ANSWERS GRADED A+
◍ Computers are machines that.
Answer: execute programs
◍ price - status0 - ""22 - "inexpensive" - "reasonable".
Answer: 68) Assuming that a user enters 22 as the price of an object, which
of the following hand-trace tables is valid for the given code snippet?int
price = 0;String status = "";Scanner in = new
Scanner(System.in);System.out.print("Please enter object's price: ");price =
in.nextInt();if (price >= 50){ status = "reasonable"; if (price >= 75) { status
= "costly"; }}else{ status = "inexpensive"; if (price <= 25) { status =
"reasonable"; }}
◍ How do you extract the first 5 characters from the string str?a) substring(str,
5)b) substring.str(0, 5) c) str.substring(5) d) str.substring(0, 5).
Answer: d) str.substring(0, 5)
◍ How do programmers find exceptions and run-time errors?.
Answer: Testing by running the program with a variety of input values
◍ what is the output of the following java program?Class Driver { public static
void main(String[] args) { int a = 3; int b = 5; if (a>b || a * 2 > b)
System.out.print(a); else System.out.print(b); }}a. 3b. 5c. 2d. 8e. none of
these.
Answer: a
◍ What is wrong with the following code snippet?int size = 42;cost =
, 9.99;System.out.println("size = " + size);System.out.println(" cost = " +
cost);a) The code snippet uses a variable that has not yet been initialized.b)
The code snippet uses a variable that has not been declared.c) The code
snippet attempts to assign a decimal value to an integer variable.d) The code
snippet attempts to assign an integer value to a decimal variable..
Answer: b) The code snippet uses a variable that has not been declared.
◍ 90) Which statement is true about the code snippet below?public static void
main(String[] args) { Scanner in = new Scanner(System.in);
ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while
(in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; }}a) The
code adds all input numbers to the array listb) The code has compile-time
errorc) The array list is full after 100 numbers are enteredd) The code has a
run-time error.
Answer: d) The code has a run-time error
◍ In order to translate a Java program to a class file, the computer needs to
have software called a(n).
Answer: compiler
◍ What kind of error is it when your program has a syntax error?.
Answer: Compile-time error
◍ 40) Which statement about instance variables is correct?a) Instance
variables must be declared within a method. b) Instance variables must be
declared inside the class, but outside any method. c) Instance variables may
be declared inside the class, or inside a method.d) Instance variables must be
declared outside the class.
Answer: b) Instance variables must be declared inside the class, but outside
any method
◍ which of the following loops would correctly add 1 to all but the first
element in values?a. for (int j = 1; j < values.length; j++) values[j]++;b. for
(int j = 0; j < values.length; j++) values[j]++;c. for (int j = 1; j <
values.length - 1; j++) values[j]++;d. for (int j = 0; j < values.length - 1; j++)
values[j]++;e. for (int j = values.length - 1; j >0; j--) values[j]++;f. for (int j
, = values.length; j >0; j--) values[j]++;g. for (int j = values.length; j >=0; j--)
values[j]++;h. for (int j = values.length - 1; j >=0; j--) values[j]++;i. none of
these.
Answer: a, e
◍ 34) What is the output of the code snippet given below?String s =
"abcde";int i = 1;do{ if (i > 1) { System.out.print(s.substring(i, i + 1));
}}while (i < 5);a) No outputb) No output (infinite loop)c) abcded) bcde.
Answer: Answer: b) No output (infinite loop)
◍ Which one of the following is a correct representation of the given
mathematical expression in Java?a + b----- 2a) a + b % 2b) a + b / 2 c) a + (b
/ 2)d) (a + b) / 2.
Answer: d) (a+b) / 2
◍ Which object oriented element is best defined as "an object should have
complete authority over its responsibilities"?.
Answer: Answered: Inheritance, Encapsulation
◍ What is the value inside the value variable at the end of the given code
snippet?public static void main(String[] args){ int value = 3; value = value -
2 * value; value++;}a) -2b) 0c) 2d) 4.
Answer: a) -2
◍ 83) Insert the missing code in the following code fragment. This fragment is
intended to implement a method to set the state of the object.public class
Motor{ public static final STOPPED = 0; public static final PAUSED = 1;
public static final RUNNING = 2; private int motorState; . . . public void
stopMotor() { ________ }}a) motorState++;b) motorState = STOPPED;c)
motorState = PAUSED;d) motorState = RUNNING;.
Answer: b) motorState = STOPPED;
◍ In order for Java to achieve protability.
Answer: compiled Java programs contain instructions for a virtual machine
◍ 31) Which of the following is the correct code snippet for throwing a pair of
dice to get a sum of the numbers on two dice between 2 and 12 with
, different probabilities?a) (int) (Math.random() * 6 + 1)b) (int)
(Math.random() * 12 + 1)c) (int) (Math.random() 6 + 1) + (int)
(Math.random() 6 + 1)d) (int) (Math.random() * 12 - 2).
Answer: Answer: c) (int) (Math.random() 6 + 1) + (int) (Math.random() 6 +
1)
◍ What is the difference between an editor and a compiler?.
Answer: An editor allows program files to be written and stored; a compiler
converts program files into an executable program
◍ 74) Which of the loop(s) test the condition after the loop body executes?
I. for loopI
I. while loopII
I. do loopa) I onlyb) II onlyc) III onlyd) II and III.
Answer: Answer: c) III only
◍ Can you have cycles in prim's algorithm?.
Answer: No!
◍ which of the following would instantiate an array of 5 integers in Java?
Choose all that apply..
Answer: new int[5]
◍ What is the output of this code snippet?int sum = 22;sum = sum +
2;System.out.print(sum); // sum = sum + 4;System.out.print(sum);a) 2424b)
2425c) 2428d) 2528.
Answer: a) 2424
◍ 34) Which type of method modifies the object on which it is invoked?a)
Constructor method. b) Static method. c) Accessor method. d) Mutator
method..
Answer: d) Mutator method.
◍ 87) What changes do you need to make in the following code snippet to
display "Let us learn Java" exactly 10 times?int i = 0;while (i <= 10){
System.out.println("Let us learn Java"); i++;}a) while (i < 9)b) while (i <
11)c) while (i < 12)d) int i = 1;.