Code Flashcards

(105 cards)

1
Q

What are the 2 types from Java?

A
  1. Value Types ( Primitive Types )
  2. Object Types ( Reference Types )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define the output

int i = 10 ; int j = 10;

if ( 10 > 9 | ( i++ > 9 ) {
     System.out.println(i) // Output 1
}

if ( 10 > 9 || ( j++ > 9 ) {
     System.out.println(j) // Output 2
}
A
  1. 11
  2. 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define a long variable

A
long a5 = 10L
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to create a new object and access the name and balance method for Student class?

A
Student john = new Student();
john.name = "John Smith"
john.balance = 12345
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to create a variable to define if 11 is greater than 12, if yes , the variable is 1, if no, the variable is -1

Use Ternary Expression

A
int i = 11 > 12 ? 1 : -1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the output for
~~~
System.out.println(‘a’ > ‘b’);
~~~

A

false

a in ASCII is 97, while b in ASCII is 98

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to define a float value?

A
float b2 = 9.9f
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the type for

double a2 = 10.1F
A
  1. Double

Still double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How many of types that an Object have?

A
  1. Countless

We can create our own types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the number range for byte value type

A

-128 to 127

8 bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the number range for short value type?

A

-32,768 to 32767

16 bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the number range for int value type?

A

-2³¹ to 2³¹ - 1

32 bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the number range for long value type?

A

-2⁶³ to 2⁶³ - 1

64 bit

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the difference between String and Char?

A
  1. We can only define 1 character in Char and a sentence in String
  2. Use “” in String and ‘’ in Char

char a = ‘A’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What’s the difference between ++i and i++?

A
  1. ++i (prefix): Increment before using the value.
  2. i++ (postfix): Increment after using the value.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the output

int i = 5;
System.out.println(++i);
A
  1. 6

i is incremented before being printed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the output

int i = 5;
System.out.println(i++);
A
  1. 5

i is printed before being incremented

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What will be the value of i and b?

int i = 5;
int b = ++i;
A
  1. i = 6
  2. b = 6
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What will be the value of i and b?

int i = 5;
int b = i++;
A
  1. i = 6
  2. b = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What does this print?

int i = 10;
System.out.println(i++);  // ?
System.out.println(++i);  // ?
A
  1. 10
  2. 12

After the first print, it was added to 11 and was added to 12 before printing the second line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

List out 8 value type primitive types

A
  1. byte
  2. short
  3. int
  4. long
  5. float
  6. double
  7. char
  8. boolean
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Why we can’t print

int y = 082504;
System.out.println(y);
A

Because Java defined it as Octal numbers ( 0 at first ) , and since there are number 8 , which is greater than 7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the output for

System.out.println(071307);
A
  1. 29383
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How to use another class in different package?

A
  1. Use Inheritance
public ClassB extends ClassA{
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Where can private attributes access?
1. In the same class
26
Where can no type attributes access? int x2
1. In the same package
27
Where can protected attributes access?
1. In the same pakage and subclass in other packages
28
Where can public attributes access?
1. Open to all
29
What is the bit size of java primitive type 'int'?
32 bits ## Footnote -2,147,483,648 to 2,147,483,647
30
What is the bit size of java primitive type 'byte'?
8 bits ## Footnote -128 to 127
31
What is the bit size of java primitive type 'short'?
16 bits ## Footnote -32,768 to 32,767
32
What is the bit size of java primitive type 'long'?
64 bit ## Footnote -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
33
What is the bit size of java primitive type 'float'?
32 bit ## Footnote 1.4e-45 to 3.4e+38
34
What is the bit size of java primitive type 'double'?
64 bit ## Footnote 4.9e-324 to 1.8e+308
35
What prefix is used to denote a hexadecimal literal in Java?
0x | 0x1AF
36
What prefix is used to denote a binary literal in Java?
0b | 0b00101
37
In Java, the `char` type is based on what character encoding standard?
Unicode
38
What is the syntax for the newline character literal in Java?
\n
39
Which Java operator is used for assignment?
=
40
Which unary operator is used for boolean negation in Java?
!
41
What are the two primary unary operators for incrementing and decrementing a value in Java?
1. ++ 2. --
42
Which two Java operators are used exclusively for binary boolean operations?
1. && 2. ||
43
What is the general syntax for the ternary operator in Java?
``` boolean_expression ? value_if_true : value_if_false; ```
44
What does the bitwise operator `~` do in Java?
It performs a unary bitwise complement (inverts all the bits).
45
What does the bitwise operator `>>>` do in Java?
It performs an unsigned right shift, filling the leftmost bits with zeros.
46
How do you explicitly cast the floating-point value `5.5` to an integer in Java?
``` int x = (int)5.5; ``` ## Footnote When the >>> operator performs its unsigned right shift, it acts on the binary representation of these integer types. Since int is a 32-bit type and long is a 64-bit type, the shift operation changes the values of these variables by moving the bit pattern to the right
47
Which method is used to convert a String like `"123"` into an `int`?
Integer.parseInt("123");
48
How would you parse the hexadecimal string `"7A"` into its integer value in Java?
`Integer.parseInt("7A", 16);`
49
Which method converts the integer `99` into its hexadecimal string representation?
`Integer.toString(99, 16);`
50
Which class from `java.util` is commonly used for reading input from sources like `System.in`?
Scanner class
51
Which `Scanner` method reads an integer from input and stops at the next whitespace?
`sc.nextInt();`
52
Which `Scanner` method reads the entire line of input, including whitespace?
`sc.nextLine();`
53
What is the standard Java statement for printing to the standard error stream (`stderr`) without a trailing newline?
`System.err.print("...");`
54
Which `String` method checks if two strings have the same sequence of characters?
`boolean equals(String other);`
55
Which `String` method returns the character at a specific index?
`char charAt(int i);`
56
What is the result of the expression `"a".compareTo("b")` in Java?
A negative integer, such as -1.
57
Which `String` method is used to divide a string into an array of substrings based on a delimiter?
`String[] split(String delim);`
58
Which method from the `java.lang.Math` class returns a random double value between 0.0 (inclusive) and 1.0 (exclusive)?
`Math.random()`
59
Which `Math` class method is used to calculate the value of a number raised to the power of another?
`Math.pow(base, exponent)`
60
What is the purpose of the `Math.ceil(NUM)` method?
It rounds a number up to the nearest integer value.
61
What distinguishes a `do-while` loop from a `while` loop in Java?
A `do-while` loop's body is guaranteed to execute at least once before the condition is checked.
62
What is the syntax to declare and initialize an array of 10 integers in Java, all set to zero?
`int[] x = new int[10];`
63
How do you find the number of elements in an array named `x`?
By accessing its `length` property: `x.length`.
64
An array where inner arrays can have different lengths, such as `int[][] x = {{1,2},{3,4,5}};`, is known as a _____ array.
ragged array
65
When an array of an object type (e.g., `String[]`) is created, what is the default value of its elements?
`null`
66
What is the syntax for a for-each loop to iterate through an `int[]` named `x`?
`for(int v:x){...}`
67
In a class definition, the `static` keyword indicates that a field or method belongs to the _________ , not to an instance of it.
class itself
68
A special method in a Java class that is called when an object is created is called a _____.
constructor
69
Within an instance method or constructor, what does the `this` keyword refer to?
It refers to the current object instance.
70
In Java, a class can inherit from another class using the _____ keyword.
`extends`
71
A class declared with the `abstract` keyword cannot be _____.
instantiated
72
If a regular (non-abstract) class extends an abstract class, what must it do regarding the abstract methods of the parent?
It must provide a concrete implementation for all inherited abstract methods.
73
A class can implement multiple _____ using the `implements` keyword.
interfaces
74
Can an interface in Java contain instance fields?
No, fields are not inherited from interfaces.
75
Given `class B extends A {}`, which of these is a valid type inference: `A x = new B();` or `B y = new A();`?
`A x = new B();` is valid. ## Footnote **Class B is a subclass of Class A ( B "is-a" A )** A x = new B(); is valid since B can always refer to its super class this is called **Upcasting**, and is a fundamental concept of polymorphism
76
What is the purpose of Generics in Java, such as in `List`?
To provide **type-safety** by allowing types (like classes and interfaces) to be parameters when defining other classes, interfaces, and methods. ## Footnote Need some code example
77
Which method in the `List` interface returns the number of elements?
`int size();`
78
What is the primary characteristic of a `Set` collection in Java?
It does not allow duplicate elements.
79
A `Map` in Java stores elements as _ pairs.
key-value
80
How can you get a `Set` of all the keys contained in a `Map`?
By calling the `keySet()` method.
81
``` public static void main(String[] args){ int l=0, w=0, c=0; Scanner s1 = new Scanner(new File("lab9e1.txt")); while(s1.hasNext()){ String line = s1.nextLine(); l++; Scanner s2 = new Scanner(line); while(s2.hasNext()){ String word = s2.next(); w++; c = c+word.length(); } } System.out.println(l+"\n"+w+"\n"+c); } ``` If the txt file is like this 25 Java Hello World Hello Java
l = 3, w = 6 , c = 25 | l (line), w (word count), c (letter count) ## Footnote `hasNext()` looks if there is any more data `nextLine()` reads all text from current cursor position until finds newline character (\n) `next()` reads and returns next token as a String
82
What is the output for second array? ``` int[] x = new int[10]; for(int i=0; i
0 0 0 0 1 2 2 3 3 4
83
What is the output for these 2? ``` System.out.println("Hello "+1+1); System.out.println("Hello "+(1+1)); ```
1. Hello 11 2. Hello 2
84
What is the output for this? ``` public class FirstJavaProject { public static void main(String[] args) { System.out.println(args[2]); } } java FirstJavaProject monday tuesday wednesday ```
wednesday
85
What does this output? ``` System.out.println(Math.pow(2, 6)); ```
64 (2^6)
86
Create a decimal format and output it System.out.println(d.format(meter))
``` DecimalFormat d = new DecimalFormat("#0.000") double meter = 0.30555 System.out.println(d.format(meter)) // Output : 0.306 ```
87
What does % calculates?
The reminder ## Footnote 47 % 10 = 4 and remainder is 7
88
What does this output? char y2 = 55; char y1 = 64 System.out.println(y1); System.out.println(y2);
1. A 2. 7
89
How to print out random number from 0 - 100?
``` double y = Math.round(Math.random() * 100); System.out.println(y); ```
90
How to receive user input 1. Int , 2. String
``` import java.util.Scanner; Scanner sc = new Scanner(System.in); int input = sc.nextInt(); // 1. String input_1 = sc.nextLine(); // 2. ```
91
How to import all package from java.util
``` import java.util.* ```
92
How to calculate factorial? (5! = 120 -> 5x4x3x2x1 )
``` public class FirstJavaProject { public static void main(String[] args) { try { Scanner sc = new Scanner(System.in); System.out.print("Enter a number: "); int numInput = sc.nextInt(); // Change the number to Positive if (numInput < 0) { System.out.println("Number is negative, changing to positive value"); numInput = Math.abs(numInput); } long ans = factorial(numInput); System.out.println("Answer : " + ans); } catch (InputMismatchException e) { System.out.println("Please print numbers only! (" + e + ")"); } } public static long factorial(int num) { if (num == 0 || num == 1) { return 1; } else { int factorial = 1; for (int i = 2; i <= num; i++) { factorial *= i; } return factorial; } } } ```
93
Write a program to sum a digit 49 = 4 + 9 = 13
``` public static int sumDigit(int num){ int sum = 0; while(num>0){ int lastDigit = num % 10; // Gets the last number sum += lastDigit; num = num / 10; // Remove the last digit } return sum; } ```
94
Write a program to perform palindrome check Racecar - True ( Backward is the same as Forward )
``` public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter a word to check palindrome : "); String input = sc.nextLine(); boolean isPalindrome = isPalindrome(input); System.out.println("Is Palindrome : " + isPalindrome); } public static boolean isPalindrome(String text){ text = text.toLowerCase(); int left = 0; int right = text.length() - 1; while(left
95
Write a program to return fibonacci sequence?
``` public static long fibonacci(int num) { // Edge case 1: n is negative if (num < 0) { throw new IllegalArgumentException("n cannot be negative."); } // Edge case 2: n is 0 or 1 if (num <= 1) { return num; } // Use 'long' to prevent overflow long a = 0; long b = 1; long temp; // We start at i=2 because we already have n=0 and n=1 for (int i = 2; i <= num; i++) { temp = a + b; // Calculate the next number a = b; // Shift b to a b = temp; // Shift the new number to b } return b; // b holds the Nth number } ```
96
Write a program to count the vowel and return a number
``` public class FirstJavaProject { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter a word to check vowel : "); String input = sc.nextLine(); int totalVowels = vowelCounter(input); System.out.println("Total vowel with word (" + input + ") = " + totalVowels); } public static int vowelCounter(String text){ text = text.toLowerCase(); int count = 0; for(int i = 0; i
97
Assume that Car, Bike, Boat extends Vehicle class with a go method, write an array and loop through the go method
Vehicle[] vehicles = {car,bike,boat}; for(Vehicle vehicle:vehicles){ vehicle.go(); }
98
Can we omit access modifier for abstract method?
Yes
99
When should we use @Override?
1. Abstract Method 2. Interface Class
100
What acces modifer is usable at abstract method?
1. public abstract void m1(); 2. abstract void m1(); 3. protected abstract void m1();
101
List out 4 of the visibility rule for method overriding
1. public (stronger than default) 2. protected (stronger than default) 3. void m1 (this is default) 4. private (illegal-error)
102
How to write code from Use Case Diagram extends relationship A <- extends - B
``` public class A extends B { } ```
103
How to write code from Use Case Diagram extends relationship A <- includes - B
``` public class A implements B { } ```
104
List out how access modifer is stated in class diagram? 1. Public 2. Private 3. Protected 4. Default
1. + 2. - 3. # 4. ~
105
How is abstract class defined in class diagram?
Italic