MAKAYAWA (PART 3) Flashcards
What is the result of the following code?
String s1 = "Hello";
String s2 = "World";
String s3 = s1.concat(s2);
System.out.println(s3);
A) Hello World
B) Hello
C) World
D) HelloWorld
D) HelloWorld
Which method is used to find the length of a string in Java?
A) length()
B) size()
C) getSize()
D) getLength()
A) length()
What will be the output of the following code?
String str = "Java Programming";
System.out.println(str.substring(5, 8));
A) Pro
B) Programming
C) gra
D) v
A) Pro
Which of the following is the correct way to create a Scanner object to read input from
the keyboard?
A) Scanner input = new Scanner(System.in);
B) Scanner input = new Scanner(System.out);
C) Scanner input = new Scanner(System.input);
D) Scanner input = new Scanner(System.key);
A) Scanner input = new Scanner(System.in);
What will be the output of the following code if the user inputs 25?
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println("You entered: " + num);
A) You entered: 25
B) 25
C) Error
D) You entered: num
A) You entered: 25
What method is used to read a string input in Java using the Scanner class?
A) next()
B) nextLine()
C) getInput()
D) input()
B) nextLine()
What is the correct way to write an array declaration and initialization in Java?
A) int arr[] = {1, 2, 3};
B) int[] arr = {1, 2, 3};
C) int arr = [1, 2, 3];
D) A and B
D) A and B
Which of the following statements is incorrect in Java?
A) int x = 10;
B) String s = Hello;
C) float f = 3.14;
D) boolean b = “true”;
D) boolean b = “true”;
Which of the following options correctly fills in the blank to declare a variable?
int (blank)_= 10;
A) number
B) int
C) main
D) class
A) number
What will be the output of the following code?
System.out.println(10 / 3);
A) 3.3333
B) 3
C) 3.0
D) Error
B) 3
What will be the output of the following code?
int x = 5;
x++;
System.out.println(x);
A) 4
B) 5
C) 6
D) Error
C) 6
What is the output of the following code?
int a = 10;
a = a + 5;
System.out.println(a);
A) 10
B) 15
C) 20
D) 5
B) 15
Which of the following is a valid variable declaration in Java?
A) int 123abc;
B) int abc123;
C) int _abc;
D) B and C
D) B and C
Which data type can hold a single character in Java?
A) int
B) char
C) String
D) boolean
B) char
What is the size of the int data type in Java?
A) 8 bits
B) 16 bits
C) 32 bits
D) 64 bits
C) 32 bits
What will be the output of the following code?
int x = 20;
if (x == 20) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
A) Equal
B) Not Equal
C) Error
D) Nothing
A) Equal
What will be the output of the following code?
int x = 10;
int y = x++;
System.out.println(y);
A) 9
B) 10
C) 11
D) Error
B) 10
Which of the following is the correct way to compare two strings in Java?
A) str1 == str2
B) str1.equals(str2)
C) str1.compareTo(str2)
D) str1 = str2
B) str1.equals(str2)
What will be the output of the following code?
int number = 5;
switch (number) {
case 1:
System.out.println("One");
break;
case 5:
System.out.println("Five");
break;
default:
System.out.println("Unknown");
}
A) One
B) Five
C) Unknown
D) Error
B) Five
What is the return type of a method that does not return any value?
A) void
B) null
C) int
D) float
A) void
What will be the output of the following code?
int i = 0;
while (i < 3) {
System.out.println(i);
i++;
}
A) 0 1 2
B) 1 2 3
C) 0 1 2 3
D) Error
A) 0 1 2
What will be the output of the following code?
for (int i = 0; i < 5; i++) {
if (i == 3) {
break;
}
System.out.println(i);
}
A) 0 1 2
B) 0 1 2 3
C) 0 1 2 3 4
D) Error
A) 0 1 2
What will be the output of the following code?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[2]);
A) 1
B) 2
C) 3
D) Error
C) 3
What will be the output of the following code?
public static void main(String[] args) {
System.out.println(add(5, 10));
}
static int add(int a, int b) {
return a + b;
}
A) 5
B) 10
C) 15
D) Error
C) 15
Which of the following allows a class to have more than one form?
A) Inheritance
B) Polymorphism
C) Abstraction
D) Encapsulation
B) Polymorphism