comp sci exam questions
What do we mean by number wrap-around? Give an example of when this happens!
Number wrap-around occurs when we try to assign a data type a value that is out of its numerical range. An example of this is if we tried to assigned the value 130 to a byte data type using casting. The range of the byte only goes to +127 so it will wrap around and produce the value -126.
Which of the following expressions is equivalent to the boolean expression !(𝑨 < 𝟓 && 𝑩 != 𝑪)
A >= 5 || B == C
What is the output from the following Java program fragment? Read it carefully! String S = ""; String T = ""; int i = 4; for (i = 1; i <= 3; i++); S = S + "!"; for (i = 1; i < 4; i++) T = T + "*"; System.out.print(S); System.out.println(T);
!***
How does the character "A" compare to the character "B"?
"A" is less than "B"
Given the following code, give an algebraic formula using n for the final value of count int count = 0; for (int a = 1; a <= n; a++) for (int b = 1; b <= n, b++ for (int c = 1; c <= n; c++) count = count + 1; for (int d = 1; d <= n; d++) count = count + 1; for (int e = 1; e <= n; e++) count = count + 1;
2 n ^ 3 + n ^ 2
Consider the following Java program : public class Compute { public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x <= 10) { if (x % 2 == 0) result += x ; ++x ; } System.out.println(result) ; } }
30
What is the output from the following Java program fragment? public static void main(String[ ] args) { int A = 10; int B = 20; update(A, B); System.out.println(A + " " + B); } public static void update(int X, int Y) { X = X + Y; Y = Y + X; System.out.println(X + " " + Y); }
30 50 10 20
Consider the following code fragment Rectangle r1 = new Rectangle(); r1.setColor(Color.blue); Rectangle r2 = r1; r2.setColor(Color.red); After the above piece of code is executed, what are the colors of r1 and r2 (in this order)?
Color.red Color.red
If a class has a private field, what has access to the field?
If a class has a private field, only methods that are members of the same class can access the private fields.
Convert the while loop in the following code segment to a for loop: int i = 100; Scanner scanner = new Scanner(System.in); while (i > 10) { System.out.println(scanner.nextInt( )); i--; }
Scanner scanner = new Scanner(System.in); for (int i = 100; i > 10; i--) { System.out.println(scanner.nextInt()); }
Which of these statements about constructors is false?
There must be exactly one constructor defined for a class
What happens when you try to compile each of the following codes? (For floating-point answers do not approximate and only include two decimal places. If the code will not run, write Error in the answer box) a) int Total; Total = 20 / 3; Result: ___ b) double total; total = 20.0 / 3.0; Result: ___ c) int total; total = 20.0/3.0; Result: ___
a) 6 b) 6.66 c) Error
When using hasNext() on a Scanner, how do we know when the end of file has been reached?
hasNext() returns a false
Which type of operator lets you manually convert a value, even if it means that a narrowing conversion will take place?
cast
You read the following statement in a Java program that compiles and executes. submarine.dive(depth); What can you say for sure?
dive must be a method.
A variable can be used before it is declared as long as we make sure to assign a value to it before it is used.
false
A variable name can begin with a number.
false
Adding a byte variable to a short variable will upgrade the byte variable to short first, perform the addition, and give a result as a short since short has a higher rank compared to byte.
false
All class methods (including static methods) have access to all instance fields even if they are declared private.
false
Both == and equals() method can be used to compare two strings in Java.
false
Comments that begin with // can be processed by javadoc.
false
Java automatically imports the lang package, which contains the Scanner class.
false
Method signature comprises the return data type, name of the method, and the arguments.
false
The fields of a class must be set to public so that other classes can access them and interact with that class.
false
The name of a Java class must begin with a capital letter, otherwise, the program will not run.
false
What is the output of the following code: class eq { public static void main(String args[ ]) { String s1 = "Hello"; String s2 = new String(s1); System.out.println(s1==s2); } }
false
Which keyword is used to declare a named constant?
final
Which if statement below is equivalent to the given switch statement (that is, produces the same output under the same conditions)? Assume the answer is a previously declared as an int variable. [Circle all that apply.] switch (answer){ case 0: System.out.print("0 entered"); break; case 1: System.out.print("1 entered"); break; case 3: System.out.print("3 entered"); break; case 5: System.out.print("5 entered"); break; default: System.out.print("Other value entered"); }
if (answer == 0) System.out.print("0 entered"); if (answer == 1) System.out.print("1 entered"); if (answer == 3) System.out.print("3 entered"); if (answer == 5) System.out.print("5 entered"); if (answer != 0 && answer != 1 && answer != 3 && answer != 5) System.out.print("Other value entered");
Write an if statement that sets the variable hours to 10 when the flag variable minimum is equal to true.
if (minimum == true) hours = 10;
Which of the following variable declaration would NOT compile in a java program?
int 1 var
The following data: 72 'A' "Hello World!" 2.8712 are all examples of
literals
Scanner class does not have a method to read input of data type char. Which Scanner method would you use to work around this?
nextLine
In Java, every complete sentence ends with a ____
semicolon
When a local variable has the same name as a field, the local variable's name is said to _______ the field's name.
shadow
Rewrite the following if-else-if statement as a switch statement: if (selection == 'A') System.out.println("You selected A."); else if (selection == 'B') System.out.println("You selected B."); else if (selection == 'C') System.out.println("You selected C."); else if (selection == 'D') System.out.println("You selected D."); else System.out.println("Not good with letters, eh?");
switch (selection) { case 'A': System.out.println("You selected A."); break; case 'B': System.out.println("You selected B."); break; case 'C': System.out.println("You selected C."); break; case 'D': System.out.println("You selected D."); break; default: System.out.println("Not good with letters, eh?"); }
int a = 2, b = 3, c = 4, d = 5; float k = 4.3f; System.out.println(-2U * (g-k) + c);
syntax error
A FileWriter object is needed along with the PrintWriter object to append to a file.
true
A Java while loop checks the condition before executing the body of the loop.
true
A flag in java is nothing more than a boolean variable.
true
A single System.out.println( ...) statement can cause two output lines to be displayed.
true
Any code that can be written with a switch-case statement could also be written with multiple if-else statements and vice versa
true
In Java, a public class called Book should be stored in a file called Book.java
true
It is possible to call a constructor inside another constructor.
true
Several System.out.print(...) statements can cause a single line of output to be displayed.
true
Variables that are of type int can be stored into double variables, but double variables cannot be conversely stored into an int variable.
true
When an if statement is nested in the if-clause of another statement, the only time the inner if statement is executed is when the boolean expression of the outer if statement is true.
true
When Java converts a lower-ranked value to a higher-ranked type, it is called ____
widening conversion