Java Quiz - Data Types and Control Statements PDF

Summary

This Java quiz focuses on data types and control statements. Key concepts including primitive data types, input/output, and control flow structures are covered.

Full Transcript

# Java Quiz ## Data Types 1. Which of the following is a primitive data type in Java? - **int** - decimal - String - Object 2. What is the size of the 'char' data type in Java? - 8 bits - **16 bits** - 32 bits - 64 bits 3. Which data type is used to store a single...

# Java Quiz ## Data Types 1. Which of the following is a primitive data type in Java? - **int** - decimal - String - Object 2. What is the size of the 'char' data type in Java? - 8 bits - **16 bits** - 32 bits - 64 bits 3. Which data type is used to store a single-precision floating-point number in Java? - int - **float** - long - byte 4. Which of the following is not a built-in data type in Java? - decimal - boolean - char - long **Correct answer:** decimal 5. What is the maximum value that can be stored in a 'byte' variable in Java? - 128 - **127** - 256 - 255 **Correct answer:** 127 6. What is the size of the 'double' data type in Java? - 32 bits - **64 bits** - 128 bits - 256 bits 7. In Java, what is the range of values that can be stored in a 'short' variable? - -128 to 127 - -32768 to 32768 - **-32,768 to 32,767** - -2,147,483,648 to 2,147,483,647 8. Which data type is used to represent whole numbers in Java? - float - **int** - double - byte 9. Which data type is used to represent true/false values in Java? - **boolean** - int - char - byte 10. What is the size of the 'boolean' data type in Java? - **1 bit** - 8 bits ## Input and Output 1. What is the purpose of the System.out.print method in Java? - Reading user input - **Writing text to the console** - Opening a file - Performing arithmetic operations **Correct answer:** Writing text to the console 2. Which class provides methods for reading input from the user in Java? - **Scanner** - System - InputReader - BufferedReader 3. What is the method used to read a string from the user using Scanner? - readLine() - readString() - **nextLine()** - getString() **Correct answer:** nextLine() 4. How can you output a new line character in Java? - **\n** - \r - \t - \\ 5. Which method is used to print the formatted output in Java? - print() - println() - **printf()** - write() 6. What is the purpose of the %n in a printf format string? - **Represents a new line character** - Represents a tab character - Represents the percentage symbol - Represents a null character 7. Which operator is used to get the remainder of a division operation in Java? - **%** - & - / - ! 8. What does the System.in represent in Java? - Standard output stream - Standard error stream - **Standard input stream** - Standard logging stream 9. What is the output of the following code? ```java int num = 10; System.out.printf("The number is: %d", num); ``` - **The number is: 10** - The number is: "num" - The number is: %d - The number is: The number is: 10 10. What is the output of the following code? ```java double value = 12.345; System.out.printf("Value: %.1f", value); ``` - **Value: 12.3** - Value: 12.345 - Value: 12.34 - Value: 12.35 ## Control Statements 1. Which control statement is used to execute a block of code repeatedly based on a given condition? - if statement - for loop - **while loop** - do-while loop **Correct answer:** while loop 2. The switch statement in Java can only be used with which data types? - **int and char** - int and float - char and float - int and double **Correct answer:** int and char 3. What will be the output of the following code? ```java int x = 5; if (x > 3) { System.out.println("x is greater than 3"); } else { System.out.println("x is not greater than 3"); } ``` - **x is greater than 3** - x is not greater than 3 4. Which control statement is used to exit a loop prematurely when a specific condition is met? - **break statement** - continue statement - return statement - exit statement 5. In a for loop, what happens if the update statement is omitted? - The loop will not execute. - An error will occur during compilation. - **The loop will run infinitely.** - The loop will execute only once. **Correct answer:** The loop will run infinitely. 6. What is the correct syntax for a switch statement in Java? - **switch(condition) {}** - switch {} - case(condition) {} - case {} 7. Which loop executes the loop body at least once, even if the condition is false? - for loop - while loop - **do-while loop** - until loop 8. What is the purpose of the 'else' statement in an if-else structure? - To handle exceptions - **To provide an alternative code block if the condition is false** - To stop the execution of the program - To restart the execution of the program 9. What will be the output of the following code? ```java int x = 10; if (x % 2 == 0) { if (x > 5) { System.out.println("x is even and greater than 5"); } else { System.out.println("x is even and not greater than 5"); } } else { System.out.println("x is odd"); } ``` - **x is even and greater than 5** - x is even and not greater than 5 - x is odd 10. What is the purpose of the 'break' statement in a loop? - To restart the loop - To skip the current iteration and move to the next one - **To force the immediate termination of the loop** - To return a value from the loop ## Algorithms 1. What is an algorithm? - A set of rules to decorate code - **A step-by-step procedure to solve a problem** - A programming language - A type of data structure 2. Why do we need algorithms? - To make code more complicated - **To optimize resource usage and reduce execution time** - To confuse other programmers - To avoid using data structures 3. What characteristic ensures that an algorithm will eventually stop after a finite number of steps?. - Input - Output - Definiteness - **Finiteness** **Correct answer:** Finiteness 4. Which characteristic ensures that the algorithm's steps can be executed using basic operations or actions? - Effectiveness - Correctness - Determinism - Feasibility **Correct answer:** Effectiveness 5. What is a critical aspect of algorithm analysis? - Memory complexity - Syntax correctness - Runtime error handling - Proper indentation **Correct answer:** Memory complexity 6. Which type of algorithm aims to find a global optimum by making locally optimal choices? - Sorting Algorithms - Searching Algorithms - **Greedy Algorithms** - Backtracking Algorithms **Correct answer:** Greedy Algorithms 7. Which algorithmic technique breaks a problem into smaller sub-problems and solves them independently? - Dynamic Programming Algorithms - **Divide and Conquer Algorithms** - Backtracking Algorithms - Greedy Algorithms 8. What ensures that an algorithm's behavior is well-defined and unambiguous? - Input - Output - **Definiteness** - Finiteness **Correct answer:** Definiteness 9. Which type of algorithm is used to find a particular element in a data structure efficiently? - Sorting Algorithms - Graph Algorithms - Dynamic Programming Algorithms - **Searching Algorithms** 10. What is the primary purpose of standardizing algorithms? - To confuse other programmers - To make code more complicated - **To improve code readability and consistency** - To avoid using data structures ## Time and Space Complexity 1. The space complexity of a recursive function is proportional to: - The number of iterations - **The number of function calls** - The number of conditional statements - The number of variables used **Correct answer:** The number of function calls 2. To verify whether a function grows faster or slower than the other function, we have some asymptotic or mathematical notations, which is: - Big Omega Ω (f) - Big Theta θ (f) - Big Oh O (f) - **All of the above** Correct answer: All of the above 3. What does it mean when we say that an algorithm X is asymptotically more efficient than Y? - X will always be a better choice for small inputs - Y will always be a better choice for small inputs - **X will always be a better choice for large inputs** - X will always be a better choice for all inputs Correct answer: X will always be a better choice for large inputs 4. Which of the following is an example of an O(1) space complexity algorithm? - Quicksort - Merge sort - **Bubble sort** - Heap sort Correct answer: Bubble sort 5. To measure Time complexity of an algorithm Big O notation is used which: - describes limiting behaviour of the function - characterizes a function based on growth of function - **upper bound on growth rate of the function** - all of the mentioned Correct answer: all of the mentioned 6. Which of the following is the most efficient algorithm in terms of time complexity? - O(n) - O(n^2) - O(n log n) - 0(2^n) Correct answer: O(n) 7. What is the time complexity of an algorithm that performs n/2 operations in the worst case scenario? - O(n) - O(log n) - O(n log n) - O(n^2) Correct answer: O(n) 8. O(log n) is? - constant asymptotic notations - **logarithmic asymptotic notations** - polynomial asymptotic notations - quadratic asymptotic notations 9. What is the time complexity of the following code: ```java int a = 0; for (i = 0; i < N; i++) { for (j = N; j > i; j--) { a = a + i + j; } } ``` - O(N) - O(N*log(N)) - O(N* Sqrt(N)) - **O(N*N)** Correct answer: O(N*N) 10. What is the space complexity of an algorithm that uses an array of size n to store the input, and another array of size m to store the output? - 0(1) - O(n) - 0(m) - **O(n + m)** Correct answer: O(n + m) ## Sieve of Eratosthenes 1. Which of the following is an optimization used in the Sieve of Eratosthenes algorithm to reduce memory usage? - Using a linked list instead of an array - **Marking only odd numbers as prime** - Using a heap instead of a stack - None of the above **Correct answer:** Marking only odd numbers as prime 2. What is the space complexity of Simple Sieve? - 0(1) - **O(n)** - O(n log n) - O(n^2) **Correct answer:** O(n) 3. Which of the following is not a step in the Sieve of Eratosthenes algorithm? - Create a boolean array of size n+1 - Mark all multiples of 2 as composite - Mark all multiples of 3 as composite - **Find the greatest common divisor of each number** **Correct answer:** Find the greatest common divisor of each number 4. What is the condition used to mark a number as composite in Simple Sieve? - The number is divisible by 2 - The number is divisible by 3 - **The number is divisible by any prime less than its square root** - The number is divisible by any prime less than itself 5. How many prime numbers less than or equal to 100 can be found using the Sieve of Eratosthenes algorithm? - 24 - **25** - 26 - 13 6. Which of the following optimizations can be applied to Simple Sieve to reduce its memory usage? - Storing only odd numbers in the sieve - Using bit packing to store the boolean flags - **Both A and B** - None of the above 7. Which of the following is a drawback of the Simple Sieve algorithm? - Requires less memory - **Requires more memory** - Takes less time to execute - Takes more time to execute 8. Which of the following data structures can be used to implement Simple Sieve efficiently? - **Array** - Linked list. - Stack - Queue 9. Which of the following algorithms is an improvement over Simple Sieve? - Segmented Sieve - Incremental Sieve - **Both A and B** - None of the above 10. How many prime numbers are there between 100 and 200? - 23 - **21** - 10 - 12 ## Segmented Sieve and Incremental Sieve 1. What is the main advantage of the segmented sieve algorithm over the traditional sieve of Eratosthenes algorithm? - a)It uses less memory. - b)It is faster for small ranges of numbers. - c)It is easier to implement. - **d)It can handle larger ranges of numbers.** Correct answer: d)It can handle larger ranges of numbers. 2. Which of the following is true about the incremental sieve? - a) It only finds prime numbers. - b) It finds both prime and composite numbers. - c) It only finds composite numbers. - d) It does not find any numbers. Correct answer: a) It only finds prime numbers. 3. What is the output of the incremental sieve when n=20 is given as input? - a) 2 3 5 7 11 13 17 - b)2 3 5 7 11 13 17 19 20 - **c)2 3 5 7 11 13 17 19** - d)2 3 5 7 12 13 17 19 4. ```java "f(): ans = 0 for i = 1 to n: for j = i; j <= n; j += i: ans += 1 print(ans)" ``` - a)0(logn) - b)0(n) - **c)O(nlogn)** - d)O(n*n) 5. What is the space complexity of the segmented sieve? - a) O(n) - b) O(n log n) - c) O(n log log n) - **d) O(sqrt(n))** 6. Which of the following is a disadvantage of the incremental sieve? - a) It requires a large amount of memory. - b) It is slower than the segmented sieve. - **c) It only works for small ranges.** - d) It can only find prime numbers. 7. Which of the following is a disadvantage of the segmented sieve? - a) It requires a large amount of memory. - b) It is slower than the incremental sieve. - c) It only works for small ranges. - d) It can only find prime numbers. **Correct answer:** a) It requires a large amount of memory. 8. What is the formula for calculating the nth prime number using the Incremental Sieve algorithm? - a) n* (n-1)+2 - **b) n*n-n+1** - c) n*n+n+1 - d) n*n+2 **Correct answer:** b) n*n-n+1 9. Which algorithm is best suited for finding all prime numbers in a small range? - a) Segmented sieve - **b) Incremental sieve** - c) Both algorithms are equally suited for this task. - d) Neither algorithm is suited for this task. **Correct answer:** b) Incremental sieve 10. What is the advantage of using the Segmented Sieve algorithm over the trial division method for prime number generation? - a) The Segmented Sieve algorithm is faster - b) The Segmented Sieve algorithm is simpler to implement - c) The Segmented Sieve algorithm can be parallellized - **d) The Segmented Sieve algorithm can generate prime numbers up to a larger limit** ## Euler's Phi Function 1. What is Euler's phi function? - A) A function that counts the number of divisors of an integer. - B) A function that counts the number of prime factors of an integer. - **C) A function that counts the number of integers less than a given integer n that are relatively prime to n.** - D) A function that counts the number of integers less than a given integer n that are divisible by n. 2. What is the relationship between the values of φ(n) and φ(p^k) for prime p and positive integer k? - A) φ(n) = p^k - 1 - B)φ(n) = p^(k-1) - C)φ(n) = p^k - **D)φ(n) = (p-1)*p^(k-1)** 3. What is the relationship between the values of φ(n) and φ(m) for coprime positive integers n and m? - A) φ(nm) = φ(n) + φ(m) - **B)φ(nm) = φ(n)φ(m)** - C)φ(nm) = φ(n) - φ(m) - D)None of the above Correct answer: B)φ(nm) = φ(n)φ(m) 4. What is the output of phi(324)? - A) 98 - 90 - **C) 108** - D) 120 Correct answer: C) 108 5. What is the φ function of the number 3? - A) 0 - B) 1 - **C) 2** - D) 3 Correct answer: φ(3) = 2 6. What is the φ function of the number 8? - A) 0 - B) 1 - C) 2 - **D) 4** Correct answer: φ(8) = 4 7. What is the value of phi(n) for n = 12 using Euler's phi function? - A) 1 - B) 2 - C) 6 - **D) 4** Correct answer: φ(12) = 4 8. What is the φ function of the number 12? - A) 0 - B) 1 - C) 2 - **D) 4** Correct answer: φ(12) = 4 9. Which of the following is a property of Euler's phi function? - A) φ(n) is always even for any positive integer n. - B) φ(n) is equal to the number of divisors of n. - **C) φ(p) = p-1 for any prime number p.** - D) φ(n) is always greater than n for any positive integer n. 10. What is the value of Euler's Totient Function for the number 2000? - A)789 - B)880 - **C)800** - D)670 Correct answer: φ(2000) = 800 ## Strobogrammatic Numbers 1. What is a Strobogrammatic Number? - A number that is divisible by 3 - **A number that reads the same when rotated 180 degrees** - A number that is a multiple of 5 - A number that contains only even digits 2. What is the strobogrammatic number that is the largest prime number? - 11 - 101 - 181 - **There is no strobogrammatic number that is prime** Correct answer: There is no strobogrammatic number that is prime 3. Which of the following is not a Strobogrammatic Number? - 818 - 1881 - 969 - **666** Correct answer: 666 4. Which of the following statements is true about Strobogrammatic Numbers? - They are only found in base 10 - They are always palindromic - **They can be used in cryptography to encode messages** - They are always even 5. A number that reads the same when rotated 180 degree What is the strobogrammatic number that is the largest prime number? - 11 - 101 - 181 - **There is no strobogrammatic number that is prime** Correct answer: There is no strobogrammatic number that is prime 6. Which of the following is not a Strobogrammatic Number? - 818 - 1881 - 969 - **666** Correct answer: 666 7. Which of the following statements is true about Strobogrammatic Numbers? - They are only found in base 10 - They are always palindromic - **They can be used in cryptography to encode messages** - They are always even Correct answer: They can be used in cryptography to encode messages 8. Which of the following is a property of strobogrammatic numbers? - They are always prime - They are always even - **They are always palindromic** - They are always odd Correct answer: They are always palindromic 9. What is an advantage of using Strobogrammatic numbers in cryptography? - They provide a higher level of security. - They enable faster encryption and decryption processes. - **They are resistant to data breaches and hacking.** - They improve the efficiency of key generation. Correct answer: They are resistant to data breaches and hacking 10. Which of the following is a disadvantage of Strobogrammatic numbers in arithmetic operations? - They lead to inaccurate results in division operations. - They require specialized algorithms for addition and subtraction. - **They cause a slowdown in multiplication and exponentiation.** - They are incompatible with decimal and fractional numbers. Correct answer: They are incompatible with decimal and fractional numbers. ## Chinese Remainder Theorem (CRT) 1. Find x such that 3x = 7 (mod 10) - x =19 (mod 10). - x = 9 (mod 11). - x = 7 (mod 10). - **x = 9 (mod 10).** **Correct answer:** x = 9 (mod 10). 2. Which of the following statements is true regarding the Chinese Remainder Theorem? - It can only be applied to solve systems of congruences with prime moduli. - **It can be applied to solve systems of congruences with coprime moduli.** - It can be applied to solve systems of congruences with any moduli. - It can only be applied to solve systems of congruences with integer moduli. 3. What will be the output of the following code? ```java int num = 8; int divisor = 3; int quotient = num / divisor; int remainder = num % divisor; System.out.println(quotient + " " + remainder); ``` - 33 - **2 2** - 12 4. Consider a number that leaves a remainder of 2 when divided by 3, a remainder of 4 when divided by 5, and a remainder of 6 when divided by 7. What is the number according to the Chinese Remainder Theorem? - 114 - 119 - 123 - 456 **Correct answer:** 114 5. What is the remainder theorem? - It is a theorem in calculus that relates to finding the remainder of a polynomial function after dividing it by another polynomial function. - **It is a theorem in number theory that states that for any integer n and any integer a, there exists a unique integer q and r such that n = aq + r, where r is the remainder.** - It is a theorem in algebra that states that if a polynomial f(x) is divided by x-a, then the remainder is equal to f(a). - It is a theorem in statistics that relates to finding the remainder of a set of data after dividing it by another set of data Correct answer: It is a theorem in number theory that states that for any integer n and any integer a, there exists a unique integer q and r such that n = aq + r, where r is the remainder. 6. The Chinese Remainder Theorem is often used in which field? - **Number theory** - Geometry 7. Which of the following is a disadvantage of the Chinese Remainder Theorem? - It requires advanced knowledge of complex numbers. - It can only be applied to linear equations. - **It has limited applicability to certain types of problems.** - It is computationally intensive and time-consuming. **Correct answer:** It has limited applicability to certain types of problems. 8. Consider a number that leaves a remainder of 2 when divided by 5, a remainder of 3 when divided by 7, and a remainder of 4 when divided by 9. What is the number according to the Chinese Remainder Theorem? - 76 - **158** - 156 - 67 **Correct answer:** 158 9. ```java x = 1 (mod 5) x = 3 (mod 7) ``` What is the smallest positive integer that satisfies these congruences using the Chinese Remainder Theorem? - 17 - 11 - 10 - 13 **Correct answer:** 17 10. In cryptography, the Chinese Remainder Theorem is used for: - Generating random numbers. - Encrypting messages. - **Decrypting messages.** - Hashing algorithms. Correct answer: Decrypting messages. ## Hourglass Sum 1. What is the shape of an hourglass in a 2D array? - Square - L-shape - T-shape - **Hourglass** **Correct answer:** Hourglass 2. How many elements does a standard hourglass in a 2D array contain? - 5 - 6 - **7** - 8 **Correct answer:** 7 3. In a 6x6 matrix, how many distinct hourglasses can be found? - 16 - 24 - 36 - 49 **Correct answer:** 16 4. Which of the following best describes the process of finding an hourglass sum in a matrix? - Calculate the sum of all elements in the matrix. - **Calculate the sum of elements in each possible hourglass shape.** - Calculate the sum of elements in each row. - Calculate the sum of elements in each column. 5. What is the time complexity of finding the maximum hourglass sum in a 6x6 matrix? - 0(1) - O(n) - **O(n^2)** - O(n^3) 6. Which of the following loops is essential in the implementation of finding an hourglass sum? - A single loop iterating over all elements. - Two nested loops iterating over rows and columns. - **Three nested loops iterating over rows, columns, and diagonals.** - A loop iterating only over diagonals. - No correct answers 7. What should be initialized before iterating through the matrix to find the maximum hourglass sum? - Minimum possible integer value - **Maximum possible integer value** - Zero - Matrix size **Correct answer:** Minimum possible integer value 8. Which of the following Java methods is most suitable for storing hourglass sums while iterating through the matrix? - ArrayList - Linked List - Priority Queue - **int variable** Correct answer: int variable 9. Which part of the hourglass is typically ignored when calculating its sum? - Top row - **Bottom row** - Center element - None Correct answer: None 10. How is the maximum hourglass sum finally determined? - By summing all hourglass sums. - By finding the minimum hourglass sum. - **By comparing each hourglass sum with the current maximum.** - By counting the total number of hourglasses. ## Maximum Elements 1. What is the time complexity of the max() method in a Priority Queue in Java? - 0(1) - O(log N) - **O(N)** - O(N log N) - Other: Correct answer: O(N) 2. In a concurrent environment, which of the following Java classes is best suited for maintaining a maximum value with safe thread operations? - **ConcurrentSkipListMap** - ConcurrentHashMap - SynchronizedList - CopyOnWriteArrayList - Other: Correct answer: ConcurrentSkipListMap 3. If you have a TreeMap in Java and you want to find the maximum key, which method would you use? - **lastKey()** - floorKey() - higherKey() - firstKey() - Other: 4. What is the primary reason to use a TreeSet when needing to maintain a maximum equilibrium in a sorted collection? - **It maintains natural ordering and does not allow duplicates.** - It is synchronized and thread-safe. - It offers constant-time performance for basic operations. - It allows null values for maximum flexibility. 5. How does the PriorityQueue class in Java determine the maximum element in a priority queue? - **By using a max heap internally.** - By using a min heap internally. - By using a balanced binary tree. - By sorting elements at runtime. - Other: Correct answer: By using a min heap internally. 6. What is the maximum size of an array in Java on a 32-bit JVM? - 2^16-1 - **2^31-1** - 2^24-1 - 2^30-1 7. Which of the following is the most efficient way to keep track of the maximum value in a dynamic array when inserting and deleting elements frequently? - Use a balanced binary search tree. - **Use a max-heap data structure.** - Use an ArrayList with sorting after each insertion. - Use a HashMap to track maximum values. Correct answer: Use a max-heap data structure. 8. When using Collections.max() method, what is the time complexity if the collection is a List? - 0(1) - O(log N) - **O(N)** - O(N log N) Correct answer: O(N) 9. In Java, which of the following classes implements the Navigable Map interface and supports operations for retrieving the maximum key? - HashMap - TreeMap - LinkedHashMap - ConcurrentHashMap **Correct answer:** TreeMap 10. Given the following code snippet, what is the expected output if the PriorityQueue is used for storing integers? ```java PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); pq.add(10); pq.add(20); pq.add(30); System.out.println(pq.peek()); ``` - 10 - 20 - **30** - 40 **Correct answer:** 30 ## Leaders in an Array 1. Which algorithm has the best time complexity for finding all leaders in an array? - O(n log n) using sorting - **O(n) using a single traversal** - O(n^2) using nested loops - O(n^2) using a priority queue **Correct answer:** O(n) using a single traversal 2. Given an array A of size n, what is the maximum number of leaders possible in the array? - 1 - n-1 - **n** - n/2 **Correct answer:** n 3. In an array with all elements being the same, how many leaders are there? - 0 - **1** - n-2 - n **Correct answer:** 1 4. Consider the array A = [2, 4, 3, 6, 5, 8]. Which of the following elements is a leader? - 2 - 4

Use Quizgecko on...
Browser
Browser