Greedy Method Approach
Greedy Method Approach
(https://play.google.com/store/apps/details?id=co.jones.cjzgt)
Introduction (#1617622154105-59ec4c41-97a6)
Module 3
1. Which of the following algorithms is the best approach for solving Huffman codes?
a) exhaustive search
b) greedy algorithm
c) brute force algorithm
d) divide and conquer algorithm
Answer: b
Explanation: Greedy algorithm is the best approach for solving the Huffman codes problem since it greedily searches for an optimal solution.
2. How many printable characters does the ASCII character set consists of?
a) 120
b) 128
c) 100
d) 98
Answer: c
Explanation: Out of 128 characters in an ASCII set, roughly, only 100 characters are printable while the rest are non-printable.
4. How many bits are needed for standard encoding if the size of the character set is X?
a) log X
b) X+1
c) 2X
d) X2
Answer: a
Explanation: If the size of the character set is X, then [log X] bits are needed for representation in a standard encoding.
5. The code length does not depend on the frequency of occurrence of characters.
a) true
b) false
Answer: b
Explanation: The code length depends on the frequency of occurrence of characters. The more frequent the character occurs, the less is the length
of the code.
Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)
7. From the following given tree, what is the code word for the character ‘a’?
a) 011
b) 010
c) 100
d) 101
Answer: a
Explanation: By recording the path of the node from root to leaf, the code word for character ‘a’ is found to be 011.
8. From the following given tree, what is the computed codeword for ‘c’?
a) 111
b) 101
c) 110
d) 011
Answer: c
Explanation: By recording the path of the node from root to leaf, assigning left branch as 0 and right branch as 1, the codeword for c is 110.
9. What will be the cost of the code if character ci is at depth di and occurs at frequency fi?
a) cifi
b) ∫cifi
c) ∑fidi
d) fidi
Answer: c
Explanation: If character ci is at depth di and occurs at frequency fi, the cost of the codeword obtained is ∑fidi.
10. An optimal code will always be present in a full tree.
a) true
b) false
Answer: a
Explanation: An optimal tree will always have the property that all nodes are either leaves or have two children. Otherwise, nodes with one child
could move up a level.
Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)
11. The type of encoding where no character code is the prefix of another character code is called?
a) optimal encoding
b) prefix encoding
c) frequency encoding
d) trie encoding
Answer: b
Explanation: Even if the character codes are of different lengths, the encoding where no character code is the prefix of another character code is
called prefix encoding.
13. What is the running time of the Huffman algorithm, if its implementation of the priority queue is done using linked lists?
a) O(C)
b) O(log C)
c) O(C log C)
d) O(C2)
Answer: d
Explanation: If the implementation of the priority queue is done using linked lists, the running time of Huffman algorithm is O(C2).
Start your Programming Journey with Python Programming which is Easy to Learn and Highly in Demand
Click Here! (https://lastmomenttuitions.com/complete-python-bootcamp/?ref=42057)
18. Consider the following graph. Using Kruskal’s algorithm, which edge will be selected first?
a) GF
b) DE
c) BE
d) BG
Answer: c
Explanation: In Krsuskal’s algorithm the edges are selected and added to the spanning tree in increasing order of their weights. Therefore, the first
edge selected will be the minimal one. So, correct option is BE.
19. Which of the following edges form minimum spanning tree on the graph using kruskals algorithm?
Kruskal’s-algorithm-questions-answers-q5
a) (B-E)(G-E)(E-F)(D-F)
b) (B-E)(G-E)(E-F)(B-G)(D-F)
c) (B-E)(G-E)(E-F)(D-E)
d) (B-E)(G-E)(E-F)(D-F)(D-G)
Answer: a
Explanation: Using Krushkal’s algorithm on the given graph, the generated minimum spanning tree is shown below.
kruskals-algorithm-questions-answers-q6
Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)
22. Kruskal’s algorithm is best suited for dense graphs than the prim’s algorithm.
a) True
b) False
Answer: b
Explanation: Prim’s algorithm outperforms the Kruskal’s algorithm in case of the dense graphs. It is significantly faster if graph has more edges
than the Kruskal’s algorithm.
Start your Programming Journey with Python Programming which is Easy to Learn and Highly in Demand
Click Here! (https://lastmomenttuitions.com/complete-python-bootcamp/?ref=42057)
27. Which of the following statement about 0/1 knapsack and fractional knapsack problem is correct?
a) In 0/1 knapsack problem items are divisible and in fractional knapsack items are indivisible
b) Both are the same
c) 0/1 knapsack is solved using a greedy algorithm and fractional knapsack is solved using dynamic programming
d) In 0/1 knapsack problem items are indivisible and in fractional knapsack items are divisible
Answer: d
Explanation: In fractional knapsack problem we can partially include an item into the knapsack whereas in 0/1 knapsack we have to either include
or exclude the item wholly.
30. Given items as {value,weight} pairs {{40,20},{30,10},{20,5}}. The capacity of knapsack=20. Find the maximum value output assuming
items to be divisible.
a) 60
b) 80
c) 100
d) 40
Answer: a
Explanation: The value/weight ratio are-{2,3,4}. So we include the second and third items wholly into the knapsack. This leaves only 5 units of
volume for the first item. So we include the first item partially.
Final value = 20+30+(40/4)=60.
Crack Job Placement Aptitude in First Attempt
Prepare for Aptitude with 50+ Videos Lectures and Handmade Notes
Click Here! (https://lastmomenttuitions.com/aptitude/?ref=42057)
31. The result of the fractional knapsack is greater than or equal to 0/1 knapsack.
a) True
b) False
Answer: a
Explanation: As fractional knapsack gives extra liberty to include the object partially which is not possible with 0/1 knapsack, thus we get better
results with a fractional knapsack.
32. The main time taking step in fractional knapsack problem is ___________
a) Breaking items into a fraction
b) Adding items into a knapsack
c) Sorting
d) Looping through sorted items
Answer: c
Explanation: The main time taking a step is to sort the items according to their value/weight ratio. It defines the time complexity of the code.
33. Given items as {value,weight} pairs {{60,20},{50,25},{20,5}}. The capacity of knapsack=40. Find the maximum value output assuming
items to be divisible and nondivisible respectively.
a) 100, 80
b) 110, 70
c) 130, 110
d) 110, 80
Answer: d
Explanation: Assuming items to be divisible-
The value/weight ratio are {3, 2, 4}.So we include the third and first items wholly. So, now only 15 units of volume are left for the second item. So
we include it partially.
Final volume = 20+60+50x(15/25)=80+30=110
Assuming items to be indivisible- In this case, we will have to leave one item due to insufficient capacity.
Final volume = 60 + 20 = 80.
Start your Machine learning & Data Science journey with Complete Hands-on Learning & doubt solving Support
Click Here! (https://lastmomenttuitions.com/python-with-machine-learning/?ref=42057)
included-mentorship/youtube-2/)
(https://lastmomenttuitions.com/course/python-zero-to-hero-covering-web-development-and-machine-learning-capstone-project-from-scratch-
included-mentorship/insta-1/)/lastmomenttuition (https://www.instagram.com/lastmomenttuition/)
(https://lastmomenttuitions.com/course/python-zero-to-hero-covering-web-development-and-machine-learning-capstone-project-from-scratch-
included-mentorship/link/)/ Last Moment Tuitions (https://in.linkedin.com/company/last-moment-
tuitions#:~:text=Last%20Moment%20Tuitions%20(LMT)%20is,others%20is%20its%20teaching%20methodology.)
(https://lastmomenttuitions.com/course/python-zero-to-hero-covering-web-development-and-machine-learning-capstone-project-from-scratch-
included-mentorship/twittrwer/)/ lastmomentdost (https://twitter.com/lastmomentdost)