0% found this document useful (0 votes)
63 views13 pages

MCQ 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views13 pages

MCQ 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MULTIPLE CHOICE QUESTION:

_______________________________________________________________________________________
STRINGS
1. Which class is used to handle strings in Java?
a. String b. StringBuilder c. StringBuffer d. Text
2. What is the result of the following code?
String s = "Hello";
System.out.println(s.length());
a. 5 b. 4 c. 6 d. Error
3. How do you concatenate two strings in Java?
a. + operator b. concat() method c. Both A and B d. append() method
4. What will be the result of the following code?
String s = "Java";
System.out.println(s.charAt(2));
a. 'v' b. ASCII of ‘v’ c. 'J' d. 'a'
5. Which method is used to convert a string to uppercase?
a. toUppercase() b. toUpperCase() c. convertUpperCase() d. uppercase()
6. What will be the output of this code snippet?
String s = "Hello World";
System.out.println(s.substring(6));
a. " World" b. "Hello" c. "Hello World" d. "World"
7. Which method checks if two strings are equal, ignoring case differences?
a. equalsIgnoreCase() b. equals() c. compareToIgnoreCase() d. compareTo()
8. What is the result of the following code?
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
a. false b. true c. Error d. null
9. Which method is used to remove whitespace from both ends of a string?
a. removeSpaces() b. strip() c. trim d. cut()
10. What does the indexOf() method return if the character is not found in the string?
a. -1 b. 0 c. null d. Exception
11. What will be the result of this code?
String s = "Java";
System.out.println(s.replace('a', 'o'));
a. "Jova" b. "Jova" c. "Jovo" d. "Jova"
12. Which method is used to split a string into an array based on a delimiter?
a. split() b. divide() c. parse() d. tokenizer()
13. What is the output of the following code?
String s = "Hello";
System.out.println(s.concat(" World"));
a. "Hello World" b. "HelloWorld" c. "Hello" d. " WorldHello"
14. How do you find the position of a substring in a string?
a. indexOf() b. find() c. search() d. positionOf()
15. What does the trim() method do?
a. Removes leading and trailing whitespace b. Converts the string to lowercase
c. Replaces all whitespace with a single space d. Adds whitespace to the beginning and end of the string
16. What will be the result of the following code?
Dwivedi Tutorials

String s = "Hello";
System.out.println(s.compareToIgnoreCase ("hello"));
a. false b. true c. 1 d. 0
17. Which method is used to check if a string contains a specific sequence of characters?
a. contains() b. find() c. has() d. search()
18. What is the result of this code?
String s = "Java Programming"; 1
System.out.println(s.substring(5, 11));

~ASHISH SIR
a. "Program" b. "Progra" c. "Programmi" d. "Progra"
19. Which method is used to get a sub part of a string starting from a specified index?
a. substring() b. slice() c. sub() d. cut()
20. What will be the output of this code?
String s = "Captain";
s = s.toLowerCase().substring(1,2);
System.out.println(s);
a. "ca" b. "ap" c. "Ca" d. "AP"
21. Which of the following is not a valid string method in Java?
a. replace() b. append() c. split() d. charAt()
22. What does the compareTo() method return?
a. An integer indicating the lexicographic difference between two strings
b. A boolean indicating if two strings are equal
c. A string after concatenation
d. A substring of the original string
23. What is the result of this code?
String s = "Java Pro Programming";
System.out.println(s.lastIndexOf('P'));
a. 6 b. 4 c. 5 d. 9
24. Which method would you use to check if a string is ending with a particular character or not?
a. isEndsWith() b. contains() c. endsWith() d. exists()
25. What does the Character.isWhitespace() method return?
a. character b. true/false c. String d. depends upon
argument
26. What is the output of the following code?
String s = "Hello";
System.out.println(s.compareTo("hello")>1);
a. true b. false c. -32 d. 0
27. What will be the result of this code?
String s = "Java";
System.out.println(s.toUpperCase()+s.substring(2,3));
a. "JAVA" b. "java" c. "JAVAA" d. "JAVAV"
28. What is the result of this code snippet?
String s = "Entertainment";
System.out.println(s.indexOf(n)+s.lastIndexOf(‘n’)+lastIndexOf(‘T’));
a. 8 b. 10 c. 17 d. error
29. What will be the output of the following code?
String s = "Programming";
System.out.println(s.substring(0,s.lastIndexOf('m')));
a. program b. Programm c. Program d. Progra
30. Which method returns a copy of the string with all occurrences of a specified substring replaced by a new substring?
a. replace() b. swap() c. remove() d. substitute()
31. What is the result of this code?
String s = "Hello";
s = s.concat(" World"+“”55);
System.out.println(s);
a. Hello World “”55 b. Hello World55 c. Hello55 d. WorldHello10
32. Which of the following methods is used to check if a string starts with a particular prefix?
Dwivedi Tutorials

a. startsWith() b. beginningWith() c. hasPrefix() d. beginsWith()


33. What will be the output of the following code?
String s = "Matter";
System.out.println(s.replace("t", "n"));
a. Matter b. Manter c. Manter d. Manner
34. System.out.prinln(“ice”.indexOf(‘c’)== “ace.lastIndexOf(‘c’));
a. false b. error c. true d. none
35. “RACECAR”. equals (s.substring (4)) returns: 2
a. false b. true c. None d. 1
~ASHISH SIR
36. Which of the methods is used to extract a set of character from a string?
a. charAt() b. trim() c. substring() d. compareTo()
37. Which of the following is used to compare two strings for their quality?
a. compareTo() b. = = c. isequals() d. equals()
38. Give the output of the following string methods:
"MISSISSIPPI".indexOf('S' , 4)+"MISSISSIPPI".lastIndexOf('I');
a. 10 b. 12 c. 15 d. 14
39. Name the function that is use to convert the string value to integer value.
a. Integer.toString() b. Integer.parseInt(String) c. String.valueOf() d. Both b and c
40. Assertion (A): Both equals() and compareTo() function use to compare two string are equal are not.
Reason (R): Both functions returns true if string are equal.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation Assertion of (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
41. The method compareTo() returns ____ when two strings are equal and in lowercase :
a. true b. 0 c. 1 d. false
42. class Check
{
public static void main()
{
String s= “RAFALE”;
String r= “”;
for(int x= ?1 ;x >=0; x--)
{
char ch = ?2 ;
r=ch+r;
}
System.out.println(r);
}
}
What will be the statement in above code at ?1?
a. 0 b. s.length-1 c. s.length()-1 d. 4
43. What will be the statement in the question 43 at ?2?
a. charAt(x) b. s.substring(x) c. x.charAt(0) d. s.charAt(x)
44. Assertion (A): The `String` class in Java is mutable.
Reason (R): Once a `String` object is created, its value cannot be changed.
(a) Both Assertion and Reason are correct, and the Reason is the correct explanation for the Assertion.
(b) Both Assertion and Reason are correct, but the Reason is not the correct explanation for the Assertion.
(c) The Assertion is correct, but the Reason is incorrect.
(d) The Assertion is incorrect, but the Reason is correct.
45. Assertion (A): double data type of value cannot be converted into int type value.
Reason (R): Conversion of higher data type of value to lower type is known as type casting.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation Assertion of (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
46. Give the output of : System.out.println(“Kala”.compareTo(“kalakand”))
Dwivedi Tutorials

a. -32 b. -4 c. 4 d. 32
47. The automatic conversion of wrapper class object to its corresponding primitive data type is termed as
a) type casting b) implicit type conversion c) auto boxing d) auto unboxing
48. The concept of converting the primitive data type to its corresponding wrapper class object.
a. Function overloading b. Autoboxing c. Auto-unboxing d.converting
49. Whish of the following is different one?
a. length() b. compareTo() c. indexOf() d. substring()
50. The method to check if a character is an alphabet or not is: 3
a. isLetter(char) b. isAlpha(char) c. isUppercase(char) d. isLowercase(char)
~ASHISH SIR
51. Statement which is use to convert a character to uppercase in a string ?
a) (String Variable).toUpperCase() b) Character.toUpperCase(character variable)
c) Character.toUpperCase(String variable) d) String.toUpperCase(character variable)
52. Which of the following is a valid auto boxing statement?
(a) int x = 10;
Integer y = x;
(b) Integer x = new Integer(15);
int y = x.intValue();
(c) double x = 20.0;
Int y = (int) x;
(d) int x = 25;
int y = Integer.valueOf(x);

ARRAYS
Multiple Choice Question:
1. What is the index of the first element in an array in Java?
a. 1 b. 0 c. -1 d. 2
2. How do you declare an array of integers in Java?
a. int []arr; b. int arr[]; c. Both A and B d. int ar[4];
3. What is the default value of an element in an integer array?
a. null b. 0 c. 1 d. -1
4. How do you initialize an array with the values 10, 20, 30, 40?
a. int[] arr = {10, 20, 30, 40}; b. int arr[] = (10, 20, 30, 40);
c. int arr = {10, 20, 30, 40}; d. int[] arr = (10, 20, 30, 40);
5. Which of the following statements is true about arrays in Java?
a. Arrays can hold elements of different data types. b. Arrays have a fixed size after initialization.
c. Array indices start at 1. d. Arrays are dynamically sized.
6. How do you access the third element of an array named arr?
a. arr[3] b. arr[2] c. arr.get(3) d. arr(3)
7. What will be the result of the following code?
int[] arr = new int[5];
System.out.println(arr[1]);
a. null b. 1 c. 0 d. Error
8. How do you find the length of an array in Java?
a. array.length() b. array.size()
c. array.getLength() d. array.length
9. What is the result of this code snippet?
int[] arr = {1, 2, 3, 4};
System.out.println(arr[2]);
a. 3 b. 4 c. 2 d. 1
10. What does the following code do?
int[] arr = new int[3];
arr[0] = 5;
arr[1] = 10;
arr[2] = 15;
a. Initializes an array with values 0, 0, 0 b. Initializes an array with values 5, 10, 15
c. Initializes an array with values 5, 10 d. Initializes an array with values 5, 10, 15, 0
Dwivedi Tutorials

11. What will be the output of this code?


int[] arr = new int[4];
arr[3] = 10;
System.out.println(arr[4]);
a. 10 b. 0 c. Error d. NullPointerException
12. How do you declare a two-dimensional array in Java?
a. int[][] ar; b. int arr[][]; c. Both A and B d. int[] arr[];
13. What is the correct way to initialize a two-dimensional array with dimensions 3x2? 4
a. int [][] arr = new arr[3][2]; b. int arr[3][2] = new int[][];
~ASHISH SIR
c. int arr = new int[3][2]; d. int[][] arr = new int[3][2];
14. What will be the output of this code?
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(arr[1][0]);
a. 3 b. 4 c. 2 d. 1
15. How can you initialize an array with the size specified by a variable?
a. int[] arr = new int[]; b. int arr[] = new int [size];
c. int arr = new int[size]; d. int[] arr = size;
16. How do you declare and initialize an array with the first 5 natural numbers?
a. int arr = {1, 2, 3, 4, 5}; b. int arr[] = new int[5]{1, 2, 3, 4, 5};
c. int arr[] = {1, 2, 3, 4, 5}; d. int[] arr = new int[5] = {1, 2, 3, 4, 5};
17. What is the output of this code snippet?
int[] arr = {5, 10, 15};
System.out.println(arr.length);
a. 3 b. 2 c. 5 d. Error
18. What is the default value of a boolean array element?
a. null b. true c. false d. 0
19. What will be the output of this code?
int[][] arr = new int[2][];
arr[0] = new int[2];
arr[1] = new int[3];
System.out.println(arr[1].length);
a. 3 b. 2 c. 5 d. Error
20. How do you access the last element of an array named arr with length n?
a. arr[n] b. arr[n-1] c. arr[1-n ] d. arr[0]
21. What does this code snippet do?
int[] arr = new int[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
for (int i : arr) {
System.out.println(i);
}
a. Prints 10, 20, 30 b. Prints 30, 20, 10 c. Prints 10 only d. Prints 20 only
22. What will be the output of the following code?
int[] arr = {4, 7, 9};
System.out.println(Math.pow(arr[1],0));
a. 7.0 b. 1 c. 3.0 d. 1.0
23. What will be the output of the following code?
int[] arr = {4, 7,5,12,81, 9};
System.out.println(Math.pow(arr[2],2)Math.sqrt(a[4]));
a. 12.0 b. 86.0 c. 34.0 d. 9.0
24. What will be the result of the following code?
int[] arr = {1, 2, 3, 4};
arr[2] = 10;
arr[1]=a[3];
for(int i=0;i<3;i++)
System.out.println(arr[2]+ “,”);
Dwivedi Tutorials

a. 1,2,3, b. 1,4,10, c. 1,2,10, d. 1,4,10,4,


25. What is the result of this code snippet?
int[] arr = {5, 10, 15};
for (int i = arr.length-2; i>0; i--) {
System.out.print(arr[i] + " ");
}
a. 10 15 b. 15 10 5 c. 10 d. 10 5
26. What is the result of this code snippet? 5
String arr[]={“fun” , “funny”, “funniest”};
~ASHISH SIR
System.out.println(arr[2].substring(0,3).equals(arr[0])+ “”.concate(arr[0]));
a. truefun b. true c. fun d. falsefun
27. What will be the output of this code?
int[] arr ={25,81,36,100,49};
arr[4] = 16;
for(int i=4;i>=0;i--)
System.out.println(Math.round(Math.sqrt(arr[i]))," ");
a. 7, 10, 6, 9,5 b. 4, 10, 6, 9,5 c. error d. 4, 7, 6, 9,5
28. Which of the following code snippets correctly initializes an array of 5 integers with values 1, 2, 3, 4, 5?
a. int[] arr = {1, 2, 3, 4, 5}; b. int arr[] = new int[5] {1, 2, 3, 4, 5};
c. int[] arr = new int[] {1, 2, 3, 4, 5}; d. int arr = {1, 2, 3, 4, 5};
29. What will be the output of this code?
int[] arr = {10, 20, 30};int []x={2,5,6};
arr=x;
System.out.println(x[1]+arr[2]);
a. 50 b. 35 c. 11 d. Error
30. What is the result of the following code?
int[] arr = {5, 10, 15, 20};
System.out.println(arr.length-arr[1]*arr[2]);
a. -60 b. -90 c. -30 d. Error
31. How do you pass an array to a method?
a. methodName(int[] arr) b. methodName(arr) c. methodName(int arr[]) d. Both A and C
32. What will be the result of this code?
int[] arr = new int[4];
arr[1] = ‘0’;
arr[3] = 9;
System.out.println(arr[2]+arr[1]);
a. 0 b. 7 c. 57 d. Error
33. What is the output of this code snippet?
int[] arr = {1, 2, 3, 4};
arr[0] = 10;
for (int i = 1; i < arr.length; i++)
System.out.print(arr[i] + " "+arr[i-1]);
a. 10 3 1 4 b. 1 2 3 4 c. 10 10 10 10 d. 2 10 3 2 4 3
34. What is the result of this code?
int[][] arr = {{1, 2}, {3, 4}};
System.out.println(arr[0][1]+arr[1][0]);
a. 2 b. 5 c. 3 d. 4
35. How do you find the total number of elements in a 2D array?
a. rows * columns b. array.length c. rows + columns d. array.size()
36. What will be the output of this code?
int[] arr = {1, 2, 3, 4};
for (int i = 0; i < arr.length; i++);
System.out.print(arr[i] + " ");
a. 1 2 3 4 b. 4 3 2 1 c. 1 2 3 d. error
37. What will be the output of this code?
int[] arr = {10, 20, 30};
arr[2] = 40;
Dwivedi Tutorials

System.out.println(arr[2]+Math.pow(arr[1],2));
a. 400.0 b. 120.0 c. 430.0 d. 430
38. How can you access the element in the second row and third column of a 2D array?
a. array[1][2] b. array[2][1] c. array[2][3] d. array[1][3]
39. What will be the output of this code?
int[][] arr = {{1, 2}, {3, 4}, {5, 6}};
System.out.println(arr[2][1]);
a. 3 b. 5 c. 4 d. 6 6
40. What is the default value of a double array element?
~ASHISH SIR
a. 0.0 b. 0 c. null d. NaN
41. What does the following code snippet do?
int[] arr = {1, 2, 3};
int[] arr2 = arr;
arr2[1] = 10;
System.out.println(arr2[1]);
a. 10 b. 2 c. 3 d. Error
42. How do you declare an array that can hold 10 elements of type double?
a. double[] arr = new double[10]; b. double arr[] = new double[10];
c. Both A and B d. double arr = new double[10];
43. What will be the output of the following code?
int[] arr = new int[5];
arr[0] = 1;
arr[4] = 5;
System.out.println(arr[1]);
a. 1 b. 0 c. 5 d. Error
44. How do you access the last element of a two-dimensional array with 3 rows and 4 columns?
a. array[2][3] b. array[3][2] c. array[3][4] d. array[2][4]
45. Assertion (A): In Java, the `for` loop can be used to iterate over arrays.
Reason (R): The `for` loop has a special syntax that allows it to iterate through arrays using an index.
(a) Both Assertion and Reason are correct, and the Reason is the correct explanation for the Assertion.
(b) Both Assertion and Reason are correct, but the Reason is not the correct explanation for the Assertion.
(c) The Assertion is correct, but the Reason is incorrect.
(d) The Assertion is incorrect, but the Reason is correct.
Explanation: The `for` loop is commonly used to iterate over arrays in Java. The loop uses an index to access each
element in the array, making the Reason a correct explanation of the Assertion.
46. Assertion (A): length() - function is used to find the length.
Reason (R): Is use to calculate the length of an array as well as String.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation Assertion of (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
47. Name the following picture depicts:
a. polymorphism b. searching c. sorting d. filtering

48. Name the following picture depicts:


a. 1-D array b. 2-D array c. searching d. sorting
Dwivedi Tutorials

49. Name the following picture depicts:


a. 1-D array b. 2-D array c. searching d. sorting
7

~ASHISH SIR
50. Name the following picture depicts:
a. 1-D array b. 2-D array c. searching d. sorting

51. What amount of size in bytes array arr will occupy, if the array elements are 2,64,45,11
a. 4 b. 16 c. 8 d. 32
52. What amount of size in bytes array arr will occupy, if the array elements are ‘e’, ‘w’ , ‘f’, ‘2’
a. 4 b. 2 c. 8 d. 1

USER-DEFINED FUNCTIONS (METHODS)

Multiple Choice Question:


1. A function in java is ______ by default?
a. private b. public c. protected d. default
2. Static functions can access
a. Static data members b. Non static data members c. Both static and non static d. None
3. An overloaded function is
a. A function with name but different return types
b. A function with name but different number of parameters
c. A function with name but different types of parameters
d. Both b and c
4. The prototype of a function take that returns an int and receive an array is
a. public void take(int a[]) b. public int take(int a) c. public int take(int a[]) d. none
5. Given code:
int Change(int p, int q)
{
p = p*q;
return p;
}
The Statement to call the above function will be
a. Change (10,20); b. int r=change (10,20); c. Change(9.5, 10.5); d. None
6. Identify the modifier which cannot be used for constructor.
a. public b. private c. protected d. static
7. Defining two or more methods in the same class with same name is called____
Dwivedi Tutorials

a. Constructor overloading b. Functions overloading


c. Static function d. None
8. The variables declared in a class for the use of all methods of the class called?
a. object b. instance variable c. reference variable d. none
9. A private function is accessible to
a. Own class only b. Subclass c. Subclass and classes in same package d. None
10. Which of the OOPS is illustrated by function overloading
a. Inheritance b. Encapsulation 8
c. Abstraction d. Polymorphism

~ASHISH SIR
11. The function call in which the data in actual parameters get changes is called ______
a. Call by value b. Call by reference c. None d. Formal call
12. Data hiding is implemented by
a. Access specifiers b. Static variables c. Inheritance d. None
13. The parameters which are used a function call statement are known as:
a. Formal parameters b. Global variables c. Instance variable d. Actual parameters
14. Which of the following methods does not change the state of an object?
a. public b. private c. pure d. impure
15. In which of the following way, the actual parameters and formal parameters shares the same location in the memory?
a. Pass by value b. Pass by reference c. Both a and b d. None
16. Which of the following access specifies allows a method to accessible anywhere in class and also outside of class?
a. Public b. Protected c. Private d. None
17. Which of the following is the default constructor of a class name local?
a. public void local () { } b. public local(int a){ }
c. private local ( int p) {} d. public void local( int a) { a1=a; }
18. Which of the following is the process of create an instance of a class?
a. Initialisation b. Instantiation c. Modification d. None
19. Specify the maximum no. of value can be returned from a function
a. only one b. Maximum two c. As many d. Depends upon condition
20. A function with private access specifier is only visible to
a. The same user only b. All user c. Inside class d. inside package
21. Assertion (A) : In java, more than one method can be created with the same name.
Reason (R) : Java implements polymorphism that allows method to be overloaded.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation of Assertion (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
22. Assertion (A) : A derived class can be based class for another class.
Reason (R) : In multilevel inheritance, derived class can serve as base class.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation of Assertion (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
23. Assertion (A): Constructor can be inherited.
Reason (R): Constructor is not the member of the class.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation of Assertion (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
24. Assertion (A): A local variable can’t use any access specifier.
Reason (R): Local variable is declared in method, constructor or block.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation Assertion of (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
25. Assertion (A): While calling method same value is copied to the formal parameter as in actual parameter in pass by
reference.
Reason (R): In pass by method both the parameters i.e. formal and actual share same memory locations.
Dwivedi Tutorials

a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation Assertion of (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
26. Assertion(A): call by value is known as pure method
Reason(R): The original value of variable does not change as operation is performed on copied values.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A) 9
c. Assertion (A) is false and Reason (R) is true
~ASHISH SIR
d. Assertion (A) is false and Reason (R) is true
27. Which of the following is the correct syntax for defining a method in Java?
a. return_type methodName(parameters) { // code }
b. methodName(parameters) { // code }
c. methodName { return_type(parameters) { // code }
d. all of the above
28. What does the keyword void signify when used in a method declaration?
a. The method returns an integer value b. The method does not return any value
c. The method returns a boolean value d. The method returns a String value
29. How do you call a method named calculate from another method?
a. calculate{}; b. calculate; c. call calculate(); d. calculate();
30. What is the output of the following code?
void printMessage() {
System.out.println("Hello, World!");
}
printMessage();
a. Hello, World! b. printMessage() c. Error d. Nothing
31. Which of the following statements is true about method parameters?
i. Parameters are optional in method definitions.
ii. A method can have multiple parameters of the same type.
iii. Parameters must be of the same type.
iv. Methods cannot have parameters.
a. only I b. both i, ii c. all of them d. none
32. What will be the result of this method call?
int add(int a, int b) {
return (++a + b);
}
System.out.println(add(5, 3));
a. 8 b. 10 c. 9 d. Error
33. Which of the following methods returns an integer value?
a. int getNumber() { return 5; } b. void getNumber() { return 5; }
c. String getNumber() { return 5; } d. boolean getNumber() { return 5; }
34. What is the result of the following method call?
int multiply(int x, int y) {
int ar[]={2,15,4,13,4};
int a=ar[0]+(int)Math.cbrt(ar[2]+ar[4])+x;
return a* y;
}
System.out.println(multiply(4, 5));
a. 20 b. 35 c. 45 d. 40
35. How do you define a method that does not take any parameters and does not return any value?
a. void methodName() { // code } b. methodName() { // code }
c. void methodName(parameters) { // code } d. return methodName() { // code }
36. What is the purpose of the return statement in a method?
a. To call another method
b. To end the method execution and return a value
c. to end the function
d. To define method parameters
Dwivedi Tutorials

37. Which method signature is valid for a method that returns a boolean value, and receives a character argument?
a. boolean isTrue() { return true; } b. boolean isTrue(char x) { return x; }
c. boolean isTrue(char cg) { // code } d. boolean isTrue(char ch) { return true; }
38. What is method overloading in Java?
a. Defining multiple methods with the same name but different parameters
b. Defining multiple methods with different names
c. Defining methods in different classes
d. Defining methods with the same name but no parameters 10

~ASHISH SIR
39. Which keyword is used to define a method that is common to all objects of a class and can be called without creating an
instance?
a. final b. public c. static d. non-static
40. Which of the following statement is correct to call another function file() from the outside class XYZ?
a. file(); b. XYZ.file(); c. importfile(); d. file,XYZ();
41. How do you define a method with default parameters in Java?
a. Java does not support default parameters directly.
b. methodName(int a = 5) { // code }
c. methodName(int a) { // code
d. methodName(int a) { a = 5; // code }
42. Which method can return a string value?
a. String getString() { return "Hello"; } b. String getString() { return 10; }
c. String getString() { // code } d. String getString() { return true; }
43. Which of the following statements is false about method calls?
a. A method must be called to execute. b. A method can be called multiple times.
c. Methods do not need to be called to execute. d. A method call can be used to execute code.
44. How would you call a static method named calculateSum from the main method?
a. main.calculateSum(); b. calculateSum.main();
c. static calculateSum(); d. calculateSum();
45. Which of the following is not a valid way to define a method?
a. public void methodName() { // code } b. private int methodName(int a) { // code }
c. static void methodName() { // code } d. All of them
46. Which of the following statements is correct about methods in Java?
a. Methods can be defined inside other methods. b. Methods can be defined outside of a class.
c. Methods are called using the . operator. d. Methods cannot have return types.
47. How do you call a method that is defined as static?
a. ClassName.methodName(); b. methodName();
c. new methodName(); d. this.methodName();
48. What is the purpose of a method's return type?
a. To specify the method's access level
b. To specify the type of value the method will return
c. To specify the method's name data type
d. To specify the method's parameters
49. How would you call a method named printDetails which is defined in the same class?
a. printDetails(); b. this.printDetails(); c. call printDetails(); d. printDetails.call();
50. What will be the output of the following code?
void greet(String name) {
System.out.println("Hello, " + name+1+1);
}
greet("Ashish");
a. Hello, b. Hello, Ashish11 c. Ashish11 d. Error
51. How do you specify a method that is accessible only within its own class?
a. private b. protected c. public d. static
52. What will be the result of this code?
double divide(double a, double b) {
return a / b;
}
System.out.println(divide(7.0, 3.0));
Dwivedi Tutorials

a. 2.3 b. 2 c. 2.3333333333333335 d. Error


53. Which method signature is correct for a method that accepts an array of integers and returns an integer?
a. int processArray(int arr) { // code } b. void processArray(int arr[]) { // code }
c. int[] processArray(int arr) { // code } d. int processArray(int[] arr) { // code }
54. Which method can be used to find the length of an array?
a. array.length b. length(array)
c. getLength(array) d. array.size()
55. How do you define a method with multiple parameters? 11
a. return_type methodName(param1; param2; ...) { // code }
~ASHISH SIR
b. return_type methodName(param1, param2, ...) { // code }
c. return_type methodName(param1 param2 ...) { // code }
d. methodName(param1, param2, ...) { return_type // code }
56. What does the static keyword indicate when used with a method?
a. The method belongs to the class rather than an instance of the class
b. The method can only be used in a static context
c. The method is abstract and needs implementation
d. The method cannot be overridden
57. Function overloading is an example of?
a. inheritance b. abstraction c. Encapsulation d. polymorphism
58. What is the correct syntax for a method that returns a boolean value?
a. boolean methodName() { return true; } b. boolean methodName() { // code }
c. methodName(boolean) { return true; } d. methodName() { return true; boolean }
59. How can you pass a value to a method?
a. By declaring it as a global variable
b. By using a return statement
c. By including it as an argument in the method call
d. By using this keyword
60. Assertion(A): call by value is known as pure method
Reason(R): The original value of variable does not change as operation is performed on copied values.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is a correct explanation of Assertion (A)
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not a correct explanation of Assertion (A)
c. Assertion (A) is false and Reason (R) is true
d. Assertion (A) is false and Reason (R) is true
61. Which of the prototype is correct way to create an object of a class name Time?
a. Time = new time (); b. Time tt=new time();
c. Time tt=new Time(); d. Time new =tt time();
62. Which among the following is the prototype of the function “sum” that takes an integer variable (x) as its argument and
returns a value of float data type
a. int sum(float x) b. float sum(int x) c.float num(int x) d.int num(float x)
63. Assertion (A): The changes made in formal parameters are not reflected back in actual parameters.
Reason (R): In call by value a copy of actual parameters is passed to formal parameters.
a. Assertion (A) and Reason (R) is true And Reason is the correction explanation of Assertion(A)
b. Assertion (A) and Reason (R) is true but Reason (R) is not the correction explanation of Assertion(A)
c. Assertion (A) is true And Reason (R) is false
d. Assertion (A) is false and Reason (R) is true
64. Assertion (A): In Java, the 'static' keyword is used to define class-level variables and methods.
Reason(R): Static members are associated with the class rather than with specific instances of the class.
(a) Both Assertion(A) and Reason (R) are true and Reason (R)is a correct explanation of Assertion(A)
(b) Both Assertion(A) and Reason (R) are true and Reason (R)is not a correct explanation of Assertion(A)
(c) Assertion (A) is true and Reason (R) is false
(d) Assertion (A) is false and Reason (R) is true
65. class Box {
int width;
Box(int w) {
width = w;
}
}
Dwivedi Tutorials

public class Test {


public static void main(String[] args) {
Box myBox = new Box(5);
System.out.println(myBox.width);
}
}
a. 0 b. 5 c. error d. none
66. A programmer attempts to compile the following Java code but encounters an error: 12
class MyClass { void MyClass() { } }
~ASHISH SIR
What is the mistake?
a. The constructor has a return type b. The class name is incorrect
c. The constructor is defined as a method d. None of the above

Encapsulation & Inheritance

Multiple Choice Question:

1. Which access modifier provides the widest access level for a class member?
a. private b. protected c. public d. default
2. What does the super keyword refer to in Java?
a. The parent class of the current object b. The subclass of the current object
c. The current object d. The superclass of the current object
3. Which of the following statements is correct about the protected access modifier?
a. It allows access within the same package and subclasses.
b. It allows access only within the same class.
c. It allows access only from subclasses in different packages.
d. It allows access from any class.
4. What is the default access level for a class member if no access modifier is specified?
a. protected b. private c. public d. default
5. Which access modifier is used to allow a class member to be accessed only within its own package and subclasses?
a. public b. private c. protected d. default
6. Assertion (A): Java supports method overloading, allowing multiple methods with the same name but
different parameters.
Reason (R): Method overloading is possible in Java because method names must be unique within a class.
(a) Both Assertion and Reason are correct, and the Reason is the correct explanation for the Assertion.
(b) Both Assertion and Reason are correct, but the Reason is not the correct explanation for the Assertion.
(c) The Assertion is correct, but the Reason is incorrect.
(d) The Assertion is incorrect, but the Reason is correct.
Explanation: Java does indeed support method overloading, where multiple methods can have the same name but
different parameter lists. The Reason is incorrect because method overloading does not require unique method names
within a class; rather, it requires different parameter lists for overloaded methods.
7. Management wants their members of company to be accessible throughout the office “Express”, what access specifier he
should use for the members:
a. Public b. Protected c. Private d. Default
8. The class which acquires the property of another class is called as
a. super class b. derived class
c. both a and b d. None of the above
9. Assertion (A): A derived class can be based class for another class.
Reason (R): In multilevel inheritance, derived class can serve as base class.
a. Both Assertion (A) and Reason (R) are true and Reason (R) is correct explanation of Assertion (A).
b. Both Assertion (A) and Reason (R) are true and Reason (R) is not correct explanation of Assertion (A).
c. Assertion (A) is true and Reason (R) is false.
d. Assertion (A) is false and Reason (R) is true.
10. Which among the following best describes the Inheritance?
a. Using the data and functions into derived segment
b. Using already defined functions in a programming language
Dwivedi Tutorials

c. Using the code already written once


d. Copying the code already written

13

~ASHISH SIR

You might also like