Programming Fundamentals

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What is the output of the following Java program statement (be exact and only include characters that will be displayed)? System.out.println("\""+"\\\"Java");

"\"Java

Evaluate the following postfix expression: 7,2,*,9,3,/,4,-,*

-14

An enqueue operation is used to remove an element from a queue data structure. true or false

false

If T1(N) = O(N log N) and T2(N) = O(log N), then T1(N) + T2(N) = O(log N). true or false

false

If a list is already sorted, the selection sort algorithm will execute faster than insertion sort. true or false

false

If a method may throw a checked exception, the code that calls this method MUST be enclosed in a try.. catch block. true or false

false

In Java, an object without any references must be explicitly deleted from memory before that portion of memory is reused. true or false

false

In the following code, title is an example of a variable with a primitive data type: String title = "Java"; true or false

false

Java comments are used by the compiler to change the behavior of a program. true or false

false

Java was created by IBM in 1990. true or false

false

Recursive methods are more efficient than iterative methods. true or false

false

Statements in the finally clause of a try statement are executed only if an exception is caught. true or false

false

Static methods can reference instance variables. true or false

false

The Java interpreter is a program that translates Java source code into bytecode. true or false

false

The for statement requires an initialization, a condition, and the increment to be specified. true or false

false

The size of an array can be changed after it is created. true or false

false

Using mergesort is always faster than insertion sort. true or false

false

When a method is called, the formal parameters are copied into the actual parameters. true or false

false

Which of the following types CANNOT be used in a switch statement? char int short float

float

Which of the following is NOT a reason to use a high-level language? It speeds up program development time It allows for a more intuitive design process It may be architecture-neutral It speeds up program execution time

it speeds up program execution time

Which Scanner method do you use if you want to read in the price of an item as a decimal and use it in a calculation? nextInt() next() nextDouble() nextLine()

nextDouble

Which of the following statements creates a pseudorandom integer in the range from 10 to 20? num1 = generator.nextInt(11) + 10; num1 = generator.nextInt(21) + 10; num1 = generator.nextInt(21); num1 = generator.nextInt(20);

num1 = generator.nextInt(11) + 10;

How many star characters, '*', will be output if we invoke the method defined below using the following statement: printStars(5); ? void printStars(int k) { for (int i=0; i<k; i++) for (k=0; k<i; i++) System.out.print("*"); }

0

What is the output of the following code ? class A { public static int changeArr(int arr) { arr = arr + 1; return arr + 2; } public static void main(String args[]) { int[] arr = {0, 1, 2}; changeArr(arr[0]); System.out.println(arr[0]); } }

0

What are going to be the values of the left (boundary) index in each iteration of a binary search, when searching for 60 in the following array: {1, 7, 15, 17, 22, 31, 60}. 3, 5, 6 0, 4, 6 3, 4, 5 0, 0, 0

0 4 6

Given the following array declaration: float[] arr = new float[9]; How many elements are in the array?

10

What is the output of the following Java code? char x = 'a', a = 'X'; if (a<x) { System.out.print("123"); if (a>'A') System.out.print("456"); } else System.out.println("789");

123456

What will be stored in variable i after the following statement is executed? int i = (int) Math.sqrt(5);

2

What is the output of the following Java code? int i=0; String s = ""; do { s += i++; } while(i<4); System.out.println(i+s);

40123

What is the output of the following code? public class Test { public static void main(String[] args) { System.out.println(foo(10)); } public static int foo(int i) { if (i<=0) return 0; if (i==1) return 1; return foo(i-1)+bar(i); } public static int bar(int i) { if (i<=0) return 0; if (i==1) return 1; return foo(i-2); } }

55

Given the following array declaration: int a[7]; What is the index of the last element in the array?

6

What is the output of the following code ? class Main { public static void main(String args[]) { System.out.println(doSomething(1,2,3)); } public static int doSomething(int x, int y, int z) { if (x<z) x = z; else x = y; return x+y+z; } }

8

What is the value of x after the execution of the following code? int x = 0; for (int i=1; i<3; i++) { for (int j=i; j<3; j++) { x += i+j; } }

9

How many times will the word NEXT be printed after the following code is executed: count1 = 1; while (count1 < 10) { count2 = 10; while (count2 <= 20) { System.out.println ("NEXT"); count2++; } count1++; }

99

What is the difference between & and && operators in Java? & is an OR, && is and AND Both are ORs, but && does short-circuit evaluation Both are ANDs, but && does short-circuit evaluation & is an AND, && is an OR

Both are ANDs, but && does short-circuit evaluation

What is the output of the following code? boolean notTrue = true; boolean notFalse = false; if (!notTrue && !notFalse) System.out.println("TRUE"); else System.out.println("FALSE");

False

What does it mean to cast a value? Change the precedence order of expression evaluation Force either a widening or a narrowing conversion Assign a value to a variable Increase the value by 1

Force either a widening or a narrowing conversion

Assume you have a linked list with a reference named front that points to the first node in the list. What type of operation is being performed by the following Java method? void op(int x) { LinkedNode newNode = new LinkedNode(x); if (front==null) { front = newNode; return; } LinkedNode cur = front; while(cur.next!=null) cur=cur.next; cur.next = newNode; } Insertion at the rear Insertion at the front Deletion from the front Deletion from the rear

Insertion at the rear

Given the following code below, what type of error do you expect to occur if you attempt to compile and run the program? float a = 7; float b = 3; System.out.println("b - a = "+(a-b)); Run-time error Logical error There are no errors in this program Compile-time error

Logical Error

What will be stored in the variable x after the loop ends? int x=2; for (int i=0; i<x; i++) { x = x + i; } 1 The loop is infinite. 0 2

Loop is infinite

Which of the following is the proper way of naming a constant representing the maximum quiz score, according to the standard Java identifier naming convention? maxQuizScore maximum_quiz_score MAX_QUIZ_SCORE maximum quiz score

MAX_QUIZ_SCORE

What is the running time of the following method in terms of its input? void foo(int N) { int k = 0; int j = 10; int sum=0; while (k<N && k<j) { sum += 5; k++; } } O(N) O(N2) O(1) O(2N)

O(1)

What is the running time of the following method in terms of its input? void bar(int K) { int Z=K*K; int count=1; for (int i=0; i<Z; i++) { count++; } } O(1) O(2K) O(K2) O(K)

O(K2)

What is the order of the following growth function expressed using Big-Oh notation: T(N)=7*N3 + N/2 + 2 * log N + 38 ? O(N/2) O(2N) O(N3 + log N) O(N3)

O(N3)

What is the running time of the following method in terms of its input? int foo(int N) { if (M<=1) return 0; else return foo(N/2) + 1; } O(N2) O(N log N) O(1) O(log N)

O(log N)

The mergesort algorithm always divides the elements in half with each recursive call. The resulting time complexity is O(n log n). What would be the time complexity if the division would occur after the first element, so that the left portion would contain 1 element and the right portion would contain the rest? O(1) O(n2) O(n log n) O(n)

O(n2)

You are writing a program that processes postfix expressions. What would be the best data structure to use for this purpose? Stack Array Queue Tree

Stack

Which of the following growth functions has the smallest asymptotic order? T(N) = N99 + 2N + 128 T(N) = N99 + 3N5 + 7N2 + N2 + 2N T(N) = 2N + 128 T(N) = 2k * 6564596546794, where k is independent of N

T(N) = 2k * 6564596546794, where k is independent of N

Write the escape sequence that is used to include a double quote in a String.

\"

What are the values of a, b, and c after the following Java code is executed? int a = 5 / 10; int b = 1 + 5 % 10; int c = ++a; a==1, b==6, c==1 a==0, b==1, c==0 a==0, b==6, c==2 a==1, b==6, c==5

a=1; b=6; c=1;

The parameter passing mechanism in Java is called: Call-by-reference Call-by-name Call-by-value-return Call-by-value

call-by-value

Which of the following types can be safely compared using the == operator? char double float String

char

What is the output of the following code? char[][] p = { {'a','b','c'}, {'e','f'}, {'g'} }; char[] t = p[2]; p[2] = p[0]; p[0] = p[1]; p[1] = t; for (int i=0; i<p.length; i++) { for (int j=0; j<p[i].length; j++) { System.out.print(p[i][j]); } }

efgabc

A method must always return a value true or false

false

Which of the following is a method of the System.out object?

println

Overloaded methods CANNOT be differentiated by: Number of parameters Types of parameters Order of parameters Return value type

return value type

A constructor method has no return type true or false

true

A private variable can be accessed anywhere within the class in which it is declared. true or false

true

A try statement can be used to handle multiple exception types true or false

true

A while statement checks the condition before entering the loop. true or false

true

An initializer list cannot be used to assign values in an array after the array was already declared and initialized. true or false

true

An object reference variable holds the address of an object true or false

true

Command-line arguments are separated using spaces. true or false

true

Elements in a stack are processed in a last in, first out manner. true or false

true

If an array is sorted, the time complexity of binary search will be O(log N). true or false

true

If the variable hold is false, x contains 5, and MAX contains 10, then what is the result of evaluating the following Boolean expression: hold || (x>MAX && !hold) || (x<MAX) true or false

true

If two object reference variables refer to the same object, then the object may be modified through either of the variables true or false

true

Java differentiates between upper case and lower case identifiers true or false

true

String objects may never be modified. true or false

true

Syntax is a set of rules that dictate how the vocabulary elements are formed to make statements. true or fasle

true

To use a class from another package, you have to either specify the fully qualified name of the class or use the import declaration. true or false

true

What is the best time complexity that can be achieved for comparison-based sorting algorithms? Ω(n log n) Ω(n2) Ω(n) Ω(1)

Ω(n log n)

What is the time complexity of the following Java method in terms of the input N? int foo(int N) { int result = 0; int C=10+N; for (int i=0; i<C; i++) for (int j=0; j<1000000; j++) for (int k=0; k<C; k++) result++; } θ(1) θ(N) θ(N2) None of the above

θ(N2)


Kaugnay na mga set ng pag-aaral

Biology: God's Living Creation ch. 8

View Set

ATI Funds Proctor Adaptive Quizzes

View Set

Diet therapy of clinical nutrition Final exam Module 1-5 ( understanding normal and clinical nutrition, tenth edition)

View Set

Chapter 32: Drug Therapy to Decrease Histamine Effects and Allergic Response

View Set

NASM 7th ed, Chapter 7: Human Movement Science

View Set

Pharmacology Practice Assessment

View Set